1.Python 版本問題:
? ?Selenium 3的話使用Python 3.6.5都可以繼續(xù)編寫,但是到了Selenium 4的時候,python 的版本需要 3.7 或 更高的版本。
2.差異:
? ?Selenium 4 移除了對舊協(xié)議的支持,并在引擎蓋下默認使用 W3C WebDriver 標準。對于大多數(shù)情況,此實施不會影響最終用戶,主要的例外是Capabilities和Actions類。在開發(fā) Selenium 3.x 版本時,實現(xiàn)了對 W3C WebDriver 標準的支持。支持這個新協(xié)議和舊的 JSON 有線協(xié)議。在 3.11 版左右,Selenium 代碼開始符合 W3C 1 級規(guī)范。最新版本的 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)
文章來源地址http://www.zghlxwxcb.cn/news/detail-620320.html
文章來源:http://www.zghlxwxcb.cn/news/detail-620320.html
到了這里,關于Python的Selenium 3 和Selenium 4的寫法區(qū)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!