2, targeting elements

Web page elements targeting a total of eight kinds:

1, id positioning using find_element_by_id () method

2, name positioning using find_element_by_name () method

3, class positioning using find_element_by_class_name () method

4, tag positioning using find_element_by_tag_name () method

  HTML itself is defined by different functions tag, such as <div>, <input>, <a> is other tag, it is difficult to distinguish between different elements by standard tag name

5, link positioning using find_element_by_link_text () method

  link positioning designed to locate hyperlinks, text link

6, partial link using positioning find_element_by_partial_link () method

  partial link targeting is a supplement to link positioning, and some text links long, we can take the position a portion of text links, as long as this part of the information can only identify this link

  Such as: "This is a very, very long text links," locate when you can write: find_element_by_partial_link ( "This is a very long")

7, XPath Location find_element_by_xpath () method

  7.1 Absolute positioning path

    Absolute positioning path is not used, the path is too long

  Using positioning element attributes 7.2

     1 driver.find_element_by_xpath('//input[@id="kw"]') 

    // current page a directory, input indication label name position elements, [@ id = 'kw'] represents the id attribute value of this element is equal to kw

    If you do not specify a label name, you can also use an asterisk (*) in place, any attribute value of the element can be used, so long as it can uniquely identify this element

     1 driver.find_element_by_xpath("//*[autocomplete='off']") 

  7.3 level combined with the property

    If an element itself can not attribute value that uniquely identifies this element, you can find it on an element, you can know that property values ​​can be uniquely identified

     1 driver.find_element_by_xpath("//span[class=soutu-btn]/input") 

  7.4 Logical Operators

    If a property can not be the only one element, consider using logical operators pay to connect multiple properties to find elements

     1 driver.find_element_by_xpath("//input[@id='kw' and @name='wd']") 

8, css positioning use: find_element_by_css_selector ()

  Css any attribute selection can be more flexible control of the positioning faster than xpath

  (.) 8.1, by targeting class attribute number is represented by:

   1 driver.find_element_by_css_selector(".s_ipt") 

  By positioning 8.2 id attribute, with (#) sign indicates:

   1 driver.find_element_by_css_selector("#kw") 

  8.3 By positioning the tag name:

. 1 driver.find_element_by_css_selector ( " INPUT " )
 2  # positioned by the parent-child relationship 
. 3 driver.find_element_by_css_selector ( " span> INPUT " )
 . 4  # by property positioned 
. 5 driver.find_element_by_css_selector ( " [name = 'kW'] " )
 . 6  # Integrated Positioning 
7 driver.find_element_by_css_selector ( " form.fm> span> input.s_ipt " )

9. By using the positioning element

  For the previous eight kinds of positioning methods, webdriver also provides another way to write, that is unified call find_element () method, to declare by By positioning method, and passing parameters corresponding to the positioning method of positioning

  By positioning when using the elements, you first need to when By category

   1 from selenium.webdriver.common.by import By  

1     driver.find_element(By.ID,"kw")
2     driver.find_element(By.NAME,"wd")
3     driver.find_element(By.CLASS_NAME,"s_ipt")
4     driver.find_element(By.TAG_NAME,"input")
5     driver.find_element(By.LINK_TEXT,"新闻")
6     driver.find_element(By.PARTIAL_LINK_TEXT,"")
7     driver.find_element(By.XPATH,"//input[@id='kw']")
8     driver.find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")

 

Guess you like

Origin www.cnblogs.com/wang-rong/p/11372882.html