千锋教育-做有情怀、有良心、有品质的职业教育机构

当前位置:首页  >  关于学院  >  技术干货  >  软件测试技术干货  >  正文

盘点selenium4和selenium3的区别

来源:千锋教育
发布时间:2022-10-14 14:04:00
分享

盘点selenium4和selenium3的区别

  在某些情况下,升级仍然会存在依赖项的问题,比如使用python的pip进行selenium安装的时候会出现依赖异常。

  一、升级依赖关系

  使用 Python 的最重要变化是所需的最低版本,Selenium 4 将至少需要Python 3.7 或更高版本。

  在python环境下,基于pip命令行做升级的话, 你可以执行:

  1、在python3.7+环境下

  执行如下命令会自动安装selenium4以上最新版。

  pip3 install selenium

  如果需要安装selenium3的相关版本,需要指定版本安装。

  pip3 install selenium==3.14.0

  如果在python3.7以上环境,但是pip版本在19以下,会出现依赖问题,而导致安装最新版selenium失败的问题,解决方法就是升级pip。

  python -m pip install --upgrade pip

  或者通过get-pip.py文件升级(私聊作者获取最新版),将其下载到本地:d:\get-pip.py

  可以通过执行该文件升级pip,主要用于pip崩溃后的重装和升级。

  python d:\get-pip.py

  2、在python3.6环境下

  执行如下命令会自动安装selenium3.14.0版本。

  pip3 install selenium

  二、新版本的差异

  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_buildcaps['name'] = my_test_namedriver = webdriver.Remote(cloud_url, desired_capabilities=caps)

  新版本的写法(selenium4+):

  2、定位元素方法的更新

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

  旧版本的写法(selenium3):

01

  以上写法在selenium4中已经失效,不能使用。

  新版本的写法(selenium4+):

02

  3、定位多个元素方法的更新

  查找多个元素 使用find_elements*。

  旧版本的写法(selenium3):

1

  新版本的写法(selenium4+):

2

  4、executable_path的更新

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

  旧版本的写法(selenium3):

  from selenium import webdriveroptions = 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 webdriverfrom selenium.webdriver.chrome.service import Service as ChromeServiceoptions = 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 Byfrom selenium.webdriver.support.relative_locator import locate_withpasswordField = 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 Byfrom selenium.webdriver.support.relative_locator import locate_withemailAddressField = 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 Byfrom selenium.webdriver.support.relative_locator import locate_withsubmitButton = 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 Byfrom selenium.webdriver.support.relative_locator import locate_withcancelButton = 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 Byfrom selenium.webdriver.support.relative_locator import locate_withemailAddressLabel = driver.find_element(By.ID, "lbl-email")emailAddressField = driver.find_element(locate_with(By.TAG_NAME, "input").near(emailAddressLabel))

声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。

相关推荐

  • 软件测试有哪些方法和技术 软件测试是在开发过程中或软件发布前,通过执行程序或系统的一系列测试活动来评估软件质量的过程。以下是一些常见的软件测试方法和技术:1.单元测试:测试软件的最小单元(如函数、方法或模块),以验证其行为是否
  • 常见的软件缺陷管理工具有哪些 以下是常见的软件缺陷管理工具:1.JIRA:JIRA是一个功能强大的项目管理和缺陷跟踪工具,支持敏捷开发和软件工程的各个方面。2.Bugzilla:Bugzilla是一个开源的缺陷跟踪系统,提供了丰富
  • 软件测试需要学习哪些内容? 软件测试是软件开发过程中至关重要的环节,它可以帮助开发人员发现和修复软件缺陷,提高软件质量和用户满意度。为了进行有效的软件测试,需要学习以下内容:
  • 软件质量模型是什么? 可维护性:软件是否容易维护、修改和扩展,并且符合软件工程的原则和标准。可移植性:软件是否能够在不同的操作系统、平台和硬件上运行,并且能够满足用户的需求和期望。软件质量模型的具体实现方式有很多种,比如ISO/IEC 9126标准、CMMI模型、IEEE 730标准等等。
  • 盘点selenium4和selenium3的区别 在某些情况下,升级仍然会存在依赖项的问题,比如使用python的pip进行selenium安装的时候会出现依赖异常。一、升级依赖关系:使用 Python 的最重要变化是所需的最低版本,Selenium 4 将至少需要Python 3.7 或更高版本。
  • 自动化测试会取代手工测试吗? 首先不管是自动化测试还是手工测试都是测试,只不过测试的方式不一样,就像走路去上班和做车去上班,你目的都是去上班,这不过一个是走路,一个是做车。那么现在问题来了,是不是有车子就不用走路啦?当然,有车子还是要走路,有自动化测试还是要有手工测试,而且手工测试是必不可少的。