Python的Selenium 3 和Selenium 4的写法区别

文章详细对比了Selenium3和Selenium4之间的差异,包括Python版本要求的提升,Selenium4对W3CWebDriver标准的默认使用,Capabilities的更新,如browserVersion和platformName等属性的变更,以及定位元素和多位元素定位方法的不同。另外,还提到了Selenium4中关于executable_path的处理方式的变化。
摘要由CSDN通过智能技术生成

1.Python 版本问题:

   Selenium 3的话使用Python 3.6.5都可以继续编写,但是到了Selenium 4的时候,python 的版本需要 3.7 或 更高的版本。

2.差异:

   Selenium 4 移除了对旧协议的支持,并在引擎盖下默认使用 W3C WebDriver 标准。对于大多数情况,此实施不会影响最终用户,主要的例外是Capabilities和Actions类。在开发 Selenium 3.x 版本时,实现了对 W3C WebDriver 标准的支持。支持这个新协议和旧的 JSON 有线协议。在 3.11 版左右,Selenium 代码开始符合 W3C 1 级规范。最新版本的 Selenium 3 中的 W3C 兼容代码将在 Selenium 4 中按预期工作。 

3.Capabilities的更新

   W3C WebDriver 标准功能列表:

  • browserName

  • browserVersion(代替version)

  • platformName(代替platform)

  • acceptInsecureCerts

  • pageLoadStrategy

  • proxy

  • timeouts

  • unhandledPromptBehavior

   Selenium 3的写法:

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

  Selenium 4的写法:

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)

  4.定位元素的写法:

     Selenium 3的写法:

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

     Selenium 4的写法:

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 3的写法在 Selenium 4中,是使用不了的。

 5.多位元素定位:

    Selenium 3的写法:

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

   Selenium 4的写法:

driver.find_elements(By.CLASS_NAME,"xx") 
# class name 相当于样式容易重复

driver.find_elements(By.CSS_SELECTOR,"xx")
# 获取css selector:右击鼠标-检查,定位到元素,
# 在弹出的elements选中的地方鼠标右击-copy-copyselector

driver.find_elements(By.ID,"xx") 
# id 可以唯一定位到一个元素(全局唯一)

driver.find_elements(By.LINK_TEXT,"xx") 
# link text 有时候不是一个输入框也不是一个按钮,
# 而是一个文字链接,例如百度搜索界面左上角的新闻,可能重复

driver.find_elements(By.NAME,"xx") 
# name 要确保是全局唯一的

driver.find_elements(By.PARITIAL_LINK_TEXT,"xx") 
# partial link text 部分链接定位,链接的部分名称,会有重复的可能。

driver.find_elements(By.TAG_NAME,"xx") 
# tag name 标签(很多),类似<div>模块,<a>,
# <link>,<span>,<input>,非常容易重复。

driver.find_elements(By.XPATH,"xx")
# 全局唯一
# 获取xpath:右击鼠标-检查,定位到元素,在弹出的elements选中的地方鼠标右击-copy-copyxpath
# xpath格式注意事项:双引号之间有双引号的时候,把里面的双引号改成单引号。
# /* 省略了前面的路径

 6.executable_path

    Selenium 3的写法:

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)

    Selenium 4的写法:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南城猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值