Selenium is a tool for automated testing and can be used in Python for automated testing.
Selenium can be superficially understood as: a Web page that simulates a person's various actions, such as clicking a button, entering information, and so on. Let's talk today about how to locate the button we want to click, or the Information box to enter the information.
-----------------------------------------------------------------------------------------------------
Browser.find_element_by_css_selector ()
This is often used: CSS is a description of HTML and XML language, I think it is most convenient to locate elements in this way, because it is convenient to find the CSS location of the element, just open the browser, in the location of the element I want to right click, review elements, and then on the top of the Firefox browser, Google At the bottom of the Chrome browser, the CSS code for the element will appear, and you can copy this directly to use the
<input id= "kw" class= "S_ipt" type= "text" maxlength= "All" name= "WD" autocomplete= "Off" >
---------------------------------------------------------------------------------------------------
BROWSER.FIND_ELEMENT_BY_ID ()
This is the page view source code, we see in the source code that the ID
---------------------------------------------------------------------------------------------------
Browser.find_element_by_tag_name ()
Browser.find_element_by_tag_name ("input")
---------------------------------------------------------------------------------------------------
Browser.find_element_by_class_name ()
Browser.find_element_by_class_name ("S_ipt")
---------------------------------------------------------------------------------------------------
Browser.find_element_by_link_text ()
OK = Webdriver. Firefox ()
Ok.get ("http://www.baidu.com")
Ok.find_element_by_link_text ("Paste"). Click ()
---------------------------------------------------------------------------------------------------
Browser.find_element_by_name ()
Browser.find_element_by_name ("WD")
---------------------------------------------------------------------------------------------------
Browser.find_element_by_partial_link_text ()
OK = Webdriver. Firefox ()
Ok.get ("http://www.baidu.com")
Ok.find_element_by_partial_link_text ("Paste"). Click ()
---------------------------------------------------------------------------------------------------
Browser.find_element_by_xpath ()
OK = Webdriver. Firefox ()
Ok.get ("http://www.baidu.com")
Ok.find_element_by_xpath ("//input[@class = ' s_ipt ']"). Send_keys (U "Hello")
Ok.find_element_by_xpath ("//input[@type = ' submit ']"). Click ()
---------------------------------------------------------------------------------------------------
If you're looking for a class of elements, we'll use the Find_elements method, which returns a list that can be iterated.
Because loading takes time, when we send a request and then we need to process the element on the new page, it is possible that the problem is due to not loading, and we can set the wait time, but I think it is not good, we can use an infinite loop to deal with this problem, for example:
While True:
Try
Browser.find_element_by_css_selector ("Input.btn"). Click ()
Except
print "Input"
Else
Break
Selenium (i): element positioning