ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

selenium4新特性

2022-06-23 01:33:08  阅读:151  来源: 互联网

标签:webdriver elements driver 特性 find selenium4 element options


新版本的差异

Selenium 4 移除了对旧协议的支持,并在引擎盖下默认使用 W3C WebDriver 标准。对于大多数情况,此实施不会影响最终用户,主要的例外是Capabilities和Actions类。

1、capabilities的更新
如果测试功能的结构不符合 W3C,可能会导致会话无法启动,以下是 W3C WebDriver 标准功能列表:

  • browserName

  • browserVersion(代替version)

  • platformName(代替platform)

  • acceptInsecureCerts

  • pageLoadStrategy

  • proxy

  • timeouts

  • unhandledPromptBehavior

上面列表中未包含的任何功能都需要包含供应商前缀。这适用于浏览器特定功能以及云供应商特定功能。例如,如果您的云供应商为您的测试使用build和name功能,您需要将它们包装在一个cloud:options块中(与您的云供应商核对适当的前缀)。

旧版本的写法(selenium3):

caps = {}
caps['browserName'] = 'firefox'
caps['platform'] = 'Windows 10'
caps['version'] = '92'
caps['build'] = my_test_build
caps['name'] = my_test_name
driver = webdriver.Remote(cloud_url, desired_capabilities=caps)

新版本的写法(selenium4+):

from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.browser_version = '92'
options.platform_name = 'Windows 10'
cloud_options = {}
cloud_options['build'] = my_test_build
cloud_options['name'] = my_test_name
options.set_capability('cloud:options', cloud_options)
driver = webdriver.Remote(cloud_url, options=options)

定位元素方法的更新

旧版本的写法(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中已经失效,不能使用。

新版本的写法(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")

定位多个元素方法的更新

查找多个元素 使用find_elements*。

旧版本的写法(selenium3):

driver.find_elements_by_class_name("className")
driver.find_elements_by_css_selector(".className")
driver.find_elements_by_id("elementId")
driver.find_elements_by_link_text("linkText")
driver.find_elements_by_name("elementName")
driver.find_elements_by_partial_link_text("partialText")
driver.find_elements_by_tag_name("elementTagName")
driver.find_elements_by_xpath("xpath")

新版本的写法(selenium4+):

driver.find_elements(By.CLASS_NAME,"xx")
driver.find_elements(By.CSS_SELECTOR,"xx")
driver.find_elements(By.ID,"xx")
driver.find_elements(By.LINK_TEXT,"xx")
driver.find_elements(By.NAME,"xx")
driver.find_elements(By.PARITIAL_LINK_TEXT,"xx")
driver.find_elements(By.TAG_NAME,"xx")


driver.find_elements(By.XPATH,"xx")

executable_path的更新

executable_path 已弃用, 请传递一个服务对象。

旧版本的写法(selenium3):

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

新版本的写法(selenium4+):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)

Selenium 4新增了相对定位

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

现在可用的相对定位有:

  • above 元素上

  • below 元素下

  • toLeftOf 元素左

  • toRightOf 元素右

  • near 附近

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

1、如何工作
Selenium是通过使用JavaScript函数返回对应元素的各种属性例如:右,左,下,上。

2、above() 元素上

返回当前指定元素位置上方的WebElement对象

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))

2、below() 元素下
返回当前指定元素位置下方的WebElement对象。


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

3、toLeftOf() 元素左
返回当前指定元素位置左方的WebElement对象。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
 
submitButton = driver.find_element(By.ID, "submit")
cancelButton = driver.find_element(locate_with(By.TAG_NAME, "button").
to_left_of(submitButton))

4、toRightOf() 元素右
返回当前指定元素位置右方的WebElement对象。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
 
cancelButton = driver.find_element(By.ID, "cancel")
submitButton = driver.find_element(locate_with(By.TAG_NAME, "button").
to_right_of(cancelButton))

4、near() 附近

from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
 
emailAddressLabel = driver.find_element(By.ID, "lbl-email")
emailAddressField = driver.find_element(locate_with(By.TAG_NAME, "input").
near(emailAddressLabel))

待补充

标签:webdriver,elements,driver,特性,find,selenium4,element,options
来源: https://www.cnblogs.com/kxtomato/p/16403798.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有