WebUI Automation, we first need to locate the elements to be manipulated in the page, and then perform various event operations, here we first describe how selenium Python locates page elements, Webdriver provides a series of methods.
Locates a single page element (returns a single Element object)
- find_element_by_id
- Find_element_by_name
- Find_element_by_xpath
- Find_element_by_link_text
- Find_element_by_partial_link_text
- Find_element_by_tag_name
- Find_element_by_class_name
- Find_element_by_css_selector
It is important to note that the above method, when matched to multiple objects, can only return the first element object anchored to, and throws an exception NoSuchElementException
when no match is reached to any element object
Positioning page Element groups (returns a list of element objects)
- 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
HTML Template Example1
<HTML> <Body> <formID= "LoginForm"> <inputname= "username"type= "text" /> <inputname= "Password"type= "Password" /> <inputname= "Continue"type= "Submit"value= "Login" /> <inputname= "Continue"type= "button"value= "Clear" /> </form></Body><HTML>
Location by ID
Login_form = driver.find_element_by_id ('loginform')
Navigate by name
Username = driver.find_element_by_name ('username'= driver.find_element_by_ Name ('password')
Positioning via XPath
Login_form = Driver.find_element_by_xpath ("/html/body/form[1]"= Driver.find _element_by_xpath ("//form[1]"= Driver.find_element_by_xpath (" //form[@id = ' loginform ']")
username = Driver.find_element_by_xpath ( " / /form[input/@name = ' username '] " ) Username = Driver.find_element_by_xpath ( " //form[@id = ' loginform ']/input[1] " ) Username = Driver.find_element_by_xpath ( //input[@name = ' username '] ")
Clear_button = Driver.find_element_by_xpath ("//input[@name = ' Continue '] [@type = ' button ']" = Driver.find_element_by_xpath ("//form[@id = ' loginform ']/input[4]")
HTML Template Example2
<HTML> <Body> <H1>Welcome</H1> <Pclass= "Content">Is you sure your want to does this?</P> <ahref= "continue.html" name= "tj_continue" title= "web" class= "RecycleBin xz">Continue</a> <ahref= "cancel.html">Cancel</a></Body><HTML>
Positioning by linking text
Continue_link = Driver.find_element_by_link_text (' Continue ') Continue_link = Driver.find_element_by_partial_link_ Text (' Conti ')
Locating by tag name
Heading1 = Driver.find_element_by_tag_name ('H1')
Through class positioning
Content = Driver.find_element_by_class_name ('content')
Positioning via CSS Selector
Content = Driver.find_element_by_css_selector ('p.content')
Continue_link = Driver.find_element_by_css_selector ("a[name=\" tj_continue\ "]")
Continue_link = Driver.find_element_by_css_selector (' a[title= "web"] ")
Continue_link = Driver.find_element_by_css_selector (' A.recyclebin ')
Selenium with Python 003-page element positioning