当前位置:网站首页>Selenium element positioning (2)

Selenium element positioning (2)

2022-07-06 01:38:00 Live up to your youth

summary

selenium 4 Relative locator is introduced ( Formerly known as friendly locator ). When it's not easy to build locators for the required elements , These locators are very helpful , But it's easy to spatially describe the position of elements relative to elements with easy to build locators .

The way

above

locate_with(by: By, using: str).above(element_or_locator:Union[WebElement, Dict] = None)

If an element is not easy to locate , But the element below this element is easy to locate , Then you can access the element below this element first , Re pass above Function to access the element .

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with

driver = webdriver.Chrome()
driver.get("http://www.example.com")
#  Navigate to the label named p The elements of 
element = driver.find_element(By.TAG_NAME, "p")
#  Relative positioning to the element, the tag name is h1 The elements of 
locator = locate_with(By.TAG_NAME, "h1").above(element)
#  Find elements based on relative positioning 
element = driver.find_element(locator)

below

locate_with(by: By, using: str).below(element_or_locator:Union[WebElement, Dict] = None)

If an element is not easy to locate , But the elements above this element are easy to locate , Then you can access the element above this element first , Re pass below Function to access the element .

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with

driver = webdriver.Chrome()
driver.get("http://www.example.com")
element = driver.find_element(By.TAG_NAME, "h1")
#  Relative positioning to element The label below is p Elements 
locator = locate_with(By.TAG_NAME, "p").below(element)
element = driver.find_element(locator)

left

locate_with(by: By, using: str).to_left_of(element_or_locator:Union[WebElement, Dict] = None)

If an element is not easy to locate , But the element on the right of this element is easy to locate , Then you can access the element on the right of the element first , Re pass to_left_of Function to access the element .

right

locate_with(by: By, using: str).to_right_of(element_or_locator:Union[WebElement, Dict] = None)

If an element is not easy to locate , But the element on the left of this element is easy to locate , Then you can access the element on the left of the element first , Re pass to_right_of Function to access the element .

near

locate_with(by: By, using: str).near(element_or_locator:Union[WebElement, Dict] = None)

If the relative positioning is not obvious , Or change according to the size of the window , have access to near Method to identify distance elements at most 50px The elements of .

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with

driver = webdriver.Chrome()
driver.get("http://www.example.com")
element = driver.find_element(By.TAG_NAME, "h1")
#  Relative positioning to element near ( It can be up and down, left and right ) The tag name of the is p The elements of 
locator = locate_with(By.TAG_NAME, "p").near(element)
element = driver.find_element(locator)
原网站

版权声明
本文为[Live up to your youth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060133468657.html