import os from selenium import webdriver from selenium.webdriver.common.keys import Keys #driver = webdriver.Chrome() chromedriver = '/Users/vital/Downloads/chromedriver' os.environ['webdriver.chrome.driver'] = chromedriver driver = webdriver.Chrome(chromedriver) 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) button = driver.find_element_by_name('submit') button.click() assert 'No results found.' not in driver.page_source #driver.close()
Click the link with the Selenium webdriver and Python
import os from selenium import webdriver chromedriver = '/Users/vital/Downloads/chromedriver' os.environ['webdriver.chrome.driver'] = chromedriver browser = webdriver.Chrome(chromedriver) browser.get('https://inventwithpython.com/') link_elem = browser.find_element_by_link_text('Read It Online') link_elem.click() # follows the 'Read It Online' link
Filling out and submitting forms with the Selenium webdriver and Python
import os from selenium import webdriver chromedriver = '/Users/vital/Downloads/chromedriver' os.environ['webdriver.chrome.driver'] = chromedriver browser = webdriver.Chrome(chromedriver) browser.get('https://gmail.com/') email_elem = browser.find_element_by_id('Email') email_elem.send_keys('user@gmail.com') next_elem = browser.find_element_by_id('next') next_elem.click() browser.pause(3000); password_elem = browser.find_element_by_id('Passwd') password_elem.send_keys('p@ssw0rd') submit_elem = browser.find_element_by_id('signIn') submit_elem.click()
Run this script in command line to install Selenium:
pip install -U selenium