The method of selecting the element selenium

The method of selecting the element selenium

· Find_element_by_css_selector: class by selecting elements, such as the <div class = 'bdy-inner'> test </ div> may be used find_element_by_css_selector ( 'div.bdy-inner').

· Find_element_by_xpath: xpath by selecting, as <form id = "loginForm"> can driver.find_element_by_xpath ( "// form [@ id = 'loginForm']").

· Find_element_by_id: id by selecting elements, such as <div id = 'bdy-inner'> test </ div> may be used driver.find_element_by_id ( 'bdy-inner').

· Find_element_by_name: name by selecting elements, such as <input name = "username" type = "text" /> may be used driver.find_element_by_name ( 'password').

· Find_element_by_link_text: by selecting the link address, such as <a href="continue.html"> Continue </a> can use driver.find_element_by_link_text ( 'Continue').

· Find_element_by_partial_link_text: by selecting a link portion of the address, such as may be used <a href="continue.html"> Continue </a> driver.find_element_by_partial_link_text ( 'Conti').

· Find_element_by_tag_name: element selected by name, such as <h1> Welcome </ h1> may be used driver.find_element_by_tag_name ( 'h1').

· Find_element_by_class_name: element selected by the class, such as <p class = "content"> Site content goes here </ p> may be used driver.find_element_by_class_name ( 'content')..

Sometimes, we need to find more than one element. Find all the above examples of comments. Thus, there are methods to select the corresponding element is the element after the above plus s, into elements.

find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

Wherein, XPath is a better method and css_selector, on the one hand relatively clear, on the other hand relative to other more accurate method of locating elements.

In the above example, we used the method of Selenium click operating element. Common operating element as follows:

· Clear: Clear content element.
· Send_keys: analog key input.
· Click: Click element.
· Submit: submit the form.

 

user = driver.find_element_by_name ( "username") # find the user name input box

user.clear # Clear user name input box contents

user.send_keys ( "1234567") # user name in the input box

pwd = driver.find_element_by_name ( "password") # find the password input box

pwd.clear # Clear password input box contents

pwd.send_keys ( "******") # Enter the password in the box

driver.find_element_by_id ( "loginBtn"). click () # Click Login

 

The code is part of a login procedure automatically taken. We can see from the code, you can perform various operations by the method of Selenium operating elements of the browser page, including login.
Selenium may be implemented in addition to simple mouse operations, may also be implemented complex double click, drag and other operations. In addition, the Selenium is also possible to obtain the size of each element of the page, you can even simulate the operation of the keyboard

Guess you like

Origin www.cnblogs.com/wenjiananquan/p/12312874.html