Selenium (i): element positioning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.