编程知识 cdmana.com

Eight elements positioning methods of selenium

UI The essence of automatic measurement is nothing more than : Positioning elements -> Operational elements -> Simulate page actions -> assertion results -> Generate test reports .
So we do UI The first step in automation is to locate elements , If you can't even locate the element, let alone the next operation .
This essay is about Selenium Eight elements positioning way , Let you successfully locate the elements .

  1. find_element_by_id

    # -*- coding:utf-8 -*-
    #  author :IT Primary school student Cai Tuotuo 
    #  Time :2020/12/13 13:42
    
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()  #  Instantiation 
    
    # demo01:
    #  Open the cloud business website 
    driver.get("http://101.133.169.100/yuns/index.php")
    #  window maximizing 
    driver.maximize_window()
    time.sleep(2)
    
    #  Click on my shopping cart to jump 
    driver.find_element_by_id('cart_num').click()
    time.sleep(2)
    
    # demo02:
    #  Open the baidu , Enter... In the search box   Editing, testing and editing , Click Baidu 
    driver.get("http://www.baidu.com")
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys(" Editing, testing and editing ")
    time.sleep(2)
    driver.find_element_by_id("su").click()
    time.sleep(3)
    
    driver.quit()
    
  2. find_element_by_name

    # -*- coding:utf-8 -*-
    #  author :IT Primary school student Cai Tuotuo 
    #  Time :2020/12/13 13:53
    
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    driver.get('http://101.133.169.100/yuns/index.php')
    
    time.sleep(2)
    
    # demo01:
    #  Enter Nike in the search box 
    #  Only inputable controls can be used send_keys() Method , such as input Control .
    driver.find_element_by_name('key').send_keys(' Nike ')
    time.sleep(2)
    
    # demo02:
    #  Clear the information in the search box 
    driver.find_element_by_name("key").clear()
    
    time.sleep(3)
    driver.quit()
    
  3. find_element_by_class_name

    # -*- coding:utf-8 -*-
    #  author :IT Primary school student Cai Tuotuo 
    #  Time :2020/12/15 16:26
    
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    
    #  Open the cloud business website 
    driver.get("http://101.133.169.100/yuns/index.php")
    driver.maximize_window()
    time.sleep(2)
    
    # demo01:
    #  Enter... In the search box   Women's wear 
    driver.find_element_by_class_name("but1").send_keys(" Women's wear ")
    time.sleep(2)
    
    # demo02:
    #  Click on the search 
    driver.find_element_by_class_name("but2").click()
    time.sleep(3)
    
    #  Be careful : If class name Is a composite class ( With spaces ), Cannot navigate to the element 
    #  You can use part of the words of the compound class to locate , But not recommended , Because it's possible to locate multiple elements 
    # driver.get("http://www.baidu.com")
    # time.sleep(2)
    # driver.find_element_by_class_name("bg s_btn btn_h btnhover")  # NoSuchElementException
    
    driver.quit()
    
  4. find_element_by_link_text

    # -*- coding:utf-8 -*-
    #  author :IT Primary school student Cai Tuotuo 
    #  Time :2020/12/15 17:44
    
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    
    driver.get("http://101.133.169.100/yuns/index.php")
    driver.maximize_window()
    time.sleep(2)
    
    # demo01:
    #  Click on  T T-shirt man 2016
    #  Match the hyperlink carrier exactly 
    driver.find_element_by_link_text("T T-shirt man 2016").click()
    time.sleep(2)
    
    # demo02;
    #  Click contact customer service 
    driver.find_element_by_link_text(" Contact customer service ").click()
    time.sleep(2)
    
  5. find_element_by_partial_link_text

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    
    driver.get("http://101.133.169.100/yuns/index.php")
    driver.maximize_window()
    time.sleep(2)
    
    # demo01:
    #  Click on  9.9 Big names 
    # partial_link_text  Fuzzy matching of hyperlinks 
    driver.find_element_by_partial_link_text(" Big names ").click()
    time.sleep(2)
    
    # demo02:
    #  Click on   Home decoration Festival 
    driver.find_element_by_partial_link_text(" Home decoration ").click()
    time.sleep(2)
    
    driver.quit()
    
  6. XPath Positioning detailed explanation and advanced method exploration

# -*- coding:utf-8 -*-
#  author :IT Primary school student Cai Tuotuo 
#  Time :2020/12/13 14:06

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Chrome()
#  Open the cloud business website 
driver.get("http://101.133.169.100/yuns/index.php")
#  window maximizing 
driver.maximize_window()
time.sleep(2)

# demo01:
#  Absolute path 
#  Search box input   Adidas 
# XPath The subscript of is from 1 At the beginning 
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[1]/form/input[1]").send_keys(" Adidas ")
time.sleep(2)

# demo02:
#  Absolute path 
#  Click on the search 
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[1]/form/input[2]").click()
time.sleep(2)

# demo03:
#  Absolute path 
#  Click on   Baba duck 
driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/dl/dd/a[31]").click()
time.sleep(2)

# demo04:
#  Relative paths 
#  Search box input   Women's wear 
# XPath The relative path is as follows  //  start , Match by attributes  @class=
driver.find_element_by_xpath("//input[@class='but1']").send_keys(" Women's wear ")
time.sleep(2)

# demo05;
#  Clear the search box 
#  Match through multiple attributes , Use and Connect 
driver.find_element_by_xpath("//input[@class='but1' and @name='key']").clear()
time.sleep(2)

# demo06:
#  Search box input   Children's wear 
#  Fuzzy matching through attributes  [contains(@ Property name ,' Property value ')]
driver.find_element_by_xpath("//input[contains(@placeholder,' Please enter ')]").send_keys(" Children's wear ")

# demo07;
#  Click on the search 
driver.find_element_by_xpath("//form/input[2]").click()
time.sleep(2)

# demo08:
#  Click on   My shopping cart 
driver.find_element_by_xpath("//div[@class='small_cart_name']/i").click()
time.sleep(2)

# demo09:
#  Click to go shopping now 
#  adopt  *  matching 
driver.find_element_by_xpath("//*[@class='r']/a").click()
time.sleep(2)

# demo10:
#  Click login   Go to the login page 
driver.find_element_by_xpath("//div/a").click()
time.sleep(2)

# demo11:
#  enter one user name :15059224492
driver.find_element_by_xpath("//div[@class='binput']/input[@name='username']").send_keys("15059224492")
time.sleep(1)

# demo12;
#  Input password :123456
driver.find_element_by_xpath("//input[@name='password']").send_keys("123456")
time.sleep(1)

# demo13:
#  Click the login button 
driver.find_element_by_xpath("//input[@name='submit']").click()
time.sleep(2)

# demo14:
#  Click on my order 
driver.find_element_by_xpath("//div[@class='help']/a[2]").click()
time.sleep(2)

# demo15:
#  Click on the shipping address 
driver.find_element_by_xpath("//div[@class='left']/dl/dd[3]/a").click()
time.sleep(2)

# demo16;
#  Click on   Add receiving address 
driver.find_element_by_xpath("//a[@id='add_address']").click()
time.sleep(2)

# demo17;
#  Enter a name : Cai Tuotuo 
driver.find_element_by_xpath("//input[@id='recive_name']").send_keys(" Cai Tuotuo ")
time.sleep(1)
#  Input mobile phone :15059224499
driver.find_element_by_xpath("//input[@name='mobile']").send_keys("15059224499")
time.sleep(1)

#  Choose province and city 
driver.find_element_by_xpath("//span[@class='area_name']").click()
time.sleep(2)
#  Choose Shaanxi Province 
driver.find_element_by_xpath("//div/a[27]").click()
time.sleep(2)
#  Move the mouse to select the province or city 
ele = driver.find_element_by_xpath("//span[@class='area_name']")
ActionChains(driver).move_to_element(ele).perform()
time.sleep(2)
#  Choose Xi'an city 
driver.find_element_by_xpath("//div[@class='conbox'][2]/a[1]").click()
time.sleep(2)
#  Move the mouse to select the province or city 
ActionChains(driver).move_to_element(ele).perform()
time.sleep(2)
#  Choose the city district 
driver.find_element_by_xpath("//div[@class='conbox'][3]/a[1]").click()
time.sleep(2)

#  Enter the detailed address 
driver.find_element_by_xpath("//input[@placeholder=' Full address of the addressee ']").send_keys(" Editing, testing and editing ")
time.sleep(1)

# demo18:
#  Click on the confirmation , Prompt saved successfully 
driver.find_element_by_xpath("//div[2]/div/div[3]/a[2]").click()
time.sleep(3)

driver.quit()
7. CSS Location explanation and advanced grammar explanation
# -*- coding:utf-8 -*-
#  author :IT Primary school student Cai Tuotuo 
#  Time :2020/12/13 14:07

from selenium import webdriver
import time

driver = webdriver.Chrome()

#  Open the cloud business website 
driver.get("http://101.133.169.100/yuns/index.php")
driver.maximize_window()
time.sleep(2)

# demo01:
#  Absolute path , Use  >  Separate , Enter children's wear in the search box 
# CSS Selector Subscript from 1 Start , Follow XPath equally 
driver.find_element_by_css_selector("html>body>div>div>div>div>form>input:nth-child(1)").send_keys(" Children's wear ")
time.sleep(2)

# demo02:
#  Absolute path , Use   Space   Separate , Click on “ Home decoration Festival ” To jump 
driver.find_element_by_css_selector("html body div.logo_bar div div.sch div.schhot a:nth-child(2)").click()
time.sleep(2)

# demo03:
#  Absolute path , Use  >  Add   Space   Separate , Click on “T T-shirt man 2016” To jump 
driver.find_element_by_css_selector("html body div.logo_bar div > div.sch > div.schhot > a:nth-child(5)").click()
time.sleep(2)

# demo04:
#  Relative paths , adopt class Selector positioning elements , Clear the text information in the search box 
# class Selectors : .
driver.find_element_by_css_selector("input.but1").clear()
time.sleep(2)

# demo05:
#  adopt class Selector positioning elements , Enter women's wear in the search box 
driver.find_element_by_css_selector(".but1").send_keys(" Women's wear ")
time.sleep(2)

# demo06:
#  adopt class Selector positioning elements , Click the search button 
driver.find_element_by_css_selector(".but2").click()
time.sleep(2)

# demo07:
#  adopt id Selector positioning elements , Click on the shopping cart 
# id Selectors : #
driver.find_element_by_css_selector("i#cart_num").click()
time.sleep(2)

# demo08:
# first-child, Click the home page to jump to the home page 
driver.find_element_by_css_selector(".help>a:first-child").click()
time.sleep(2)

# demo09:
#  adopt id Selector positioning elements , Click on the shopping cart 
driver.find_element_by_css_selector("#cart_num").click()
time.sleep(2)

# demo10:
# last-child, Click contact customer service to jump 
driver.find_element_by_css_selector(".help>a:last-child").click()
time.sleep(2)

# demo11:
#  Locate... By attributes , Pay attention to and XPath difference , Enter slippers in the search box 
driver.find_element_by_css_selector("input[placeholder=' To find your keyword ']").send_keys(" slippers ")
time.sleep(2)

# demo12:
#  Locate through multiple attributes , Pay attention to and XPath difference , Clear the text information in the search box 
driver.find_element_by_css_selector("input[name='key'][class='but1']").clear()
time.sleep(2)

# demo13:
#  In reverse order  nth-last-child(), Click the home page to jump 
driver.find_element_by_css_selector(".help>a:nth-last-child(3)").click()
time.sleep(2)

# demo14:
#  Enter shoes in the search box 
driver.find_element_by_css_selector("div[class='schbox']>form>input:nth-child(1)").send_keys(" Gym shoes ")

# demo15:
# css_selector  Don't distinguish between a dry son and a real son ,
#  If there are multiple peer labels under a label , Although these peer labels are tag name Dissimilarity , But they are sorted together 
#  Open the baidu , Type in the search box   Editing, testing and editing  , Click Baidu 
driver.get("http://www.baidu.com")
driver.maximize_window()
time.sleep(3)
driver.find_element_by_css_selector("form#form>span:nth-child(8)>input").send_keys(" Editing, testing and editing ")
driver.find_element_by_css_selector("form#form>span:nth-child(9)>input").click()
time.sleep(3)

# demo16;
#  Click the first search result to enter the official website of editing, testing and editing 
driver.find_element_by_css_selector("div[class='result c-container new-pmd'][id='1']>h3>a").click()
time.sleep(3)
#  List all handles 
handles = driver.window_handles
print(handles)
#  Print current handle 
print(driver.current_window_handle)
#  The index of the first window is 0
driver.switch_to.window(handles[1])
time.sleep(2)

# demo17:
#  Click test forum 
driver.find_element_by_css_selector("div[class='nexnav']>ul>:nth-child(4)>a").click()
time.sleep(3)

# demo18:
#  Click on   Compiling, testing, editing and learning software practical training course learning Syllabus 
driver.find_element_by_css_selector("#portal_block_48_content>h5>a").click()
time.sleep(5)

#  Close the current window 
driver.close()
time.sleep(3)

#  Close all windows 
driver.quit()
  1. find_elements_by_tag_name

    tag = driver.find_elements_by_tag_name("input")
    

版权声明
本文为[It primary school student Cai Tuotuo]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224214721830d.html

Scroll to Top