The 8 elements commonly used in selenium are located in the following ways (code can be run directly):
# coding:utf-8# First step import required module from selenium import Webdriverimport time# The second step to open the browser driver = Webdriver. Firefox () # Third step open Baidu Driver.get ("http://www.baidu.com") # 1. Manipulate the Browser object-iddriver.find_element_by_id ("kw"). Send_keys ("by_id") Time.sleep (1) # Empty text box driver.find_element_by_id ("kw") . Clear () # 2. Manipulate the Browser object-namedriver.find_element_by_name ("WD"). Send_keys ("By_name") time.sleep (1) driver.find_element_by_id ("kw") . Clear () # 3. Manipulate the Browser object-class_namedriver.find_element_by_class_name ("S_ipt"). Send_keys ("By_class_name") time.sleep (1) driver.find_element_by_id ("kw"). Clear () # 4. Operation Browser Object-tag_name, tag with the same name many, run definitely error, so comment off does not run. #driver. Find_element_by_tag_name ("Input"). Send_keys ("By_tag_name") # 5. Manipulate the Browser object-link_textdriver.find_element_by_link_text ("hao123"). Click () time.sleep (1) # Fallback to previous page Driver.back () Time.sleep (1) # 6. Manipulate the Browser object-partial_link_textdriver.find_element_by_partial_link_text ("ao123"). Click () time.sleep (1) driver.back () Time.sleep (1) # 7. Manipulate the Browser object-xpathdriver.find_element_by_xpath (".//*[@id = ' kw ']"). Send_keys ("By_xpath") time.sleep (1) driver.find_element_by_id ("kw"). Clear () # 8. Manipulate the Browser object-xpathdriver.find_element_by_css_selector ("#kw"). Send_keys ("By_css_selector") Time.sleep (1) # exit, Close is used to close the current window, quit to end the process, close all windows, and when the test is finished, use Quitdriver.close () Driver.quit ()
Selenium + Python (2)--Common 8 element positioning