美文网首页
Selenium4新特性

Selenium4新特性

作者: 开心的小哈 | 来源:发表于2022-08-08 20:23 被阅读0次

定位元素方法的更新

旧版本的写法(selenium3):

driver.find_element_by_class_name("className")
driver.find_element_by_css_selector(".className")
driver.find_element_by_id("elementId")
driver.find_element_by_link_text("linkText")
driver.find_element_by_name("elementName")
driver.find_element_by_partial_link_text("partialText")
driver.find_element_by_tag_name("elementTagName")
driver.find_element_by_xpath("xpath")

新版本的写法(selenium4+):

from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME,"xx")
driver.find_element(By.CSS_SELECTOR,"xx")
driver.find_element(By.ID,"xx")
driver.find_element(By.LINK_TEXT,"xx")
driver.find_element(By.NAME,"xx")
driver.find_element(By.PARITIAL_LINK_TEXT,"xx")
driver.find_element(By.TAG_NAME,"xx")
driver.find_element(By.XPATH,"xx")

新增了相对定位

在Selenium 4中带来了相对定位这个新功能,在以前的版本中被称之为"好友定位 (Friendly Locators)"。它可以帮助你通过某些元素作为参考来定位其附近的元素。

现在可用的相对定位有:

above 元素上

below 元素下

toLeftOf 元素左

toRightOf 元素右

near 附近

findElement 方法现在支持with(By)新方法其可返回RelativeLocator相对定位对象。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
 
passwordField = driver.find_element(By.ID, "password")
emailAddressField = driver.find_element(locate_with(By.TAG_NAME, "input").above(passwordField))

相关文章

网友评论

      本文标题:Selenium4新特性

      本文链接:https://www.haomeiwen.com/subject/esfgwrtx.html