Control browsers with selenium modules
Start a selenium controlled browser
from selenium import webdriver brower = webdriver.Firefox() brower.get('http://www.baidu.com')
Find elements on the page
from selenium import webdriver browser = webdriver.Firefox() browser.get('http://inventwithpython.com') try: elem = browser.find_element_by_class_name('row') print('found <%s> element with that class name '%(elem.tag_name)) except: print('was not able to find an element with that name.')
click page
from selenium import webdriver browser = webdriver.Firefox() browser.get('http://inventwithpython.com') linkElem = browser.find_element_by_link_text('Blog') linkElem.click()
Fill out and submit the form
from selenium import webdriver browser = webdriver.Firefox() browser.get('https://www.henanjianhua.com/wp-admin/') usernameElem = browser.find_element_by_id('user_login') usernameElem.send_keys('liu') pawElem = browser.find_element_by_id('user_pass') pawElem.send_keys('123456789') pawElem.submit()
send special key
from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() browser.get('http://nostarch.com') htmlElem.send_keys(Keys.END) htmlElem.send_keys(Keys.HOME)
Click the browser button
Using the following method,
Selenium can also simulate clicking various browser buttons:
browser.back() hits the "back" button.
browser.forward() clicks the "forward" button.
browser.refresh() hits the "refresh" button.
browser.quit() clicks the "Close Window" button.