selenium python web scraping code example
Example 1: selenium webdriver python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
Example 2: python selenium web scraping example
#Python example - use chrome driver to open google url and enter into search bar "Why is python so awesome"
#required imports
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#open driver
PATH_TO_DRIVER = './chromedriver'
driver = webdriver.Chrome(executable_path=PATH_TO_DRIVER)
#launch url using driver
driver.get('https://google.com')
#find element to manipulate e.g. by element name. 'q' is the element name of the google search bar
element = driver.find_element_by_name('q')
#write text into search bar
element.send_keys('Why is python so awesome')
#simulates 'Enter' key
element.send_keys(Keys.ENTER)