千鋒教育-做有情懷、有良心、有品質的職業教育機構
在某些情況下,升級仍然會存在依賴項的問題,比如使用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):
以上寫法在selenium4中已經失效,不能使用。
新版本的寫法(selenium4+):
3、定位多個元素方法的更新
查找多個元素 使用find_elements*。
舊版本的寫法(selenium3):
新版本的寫法(selenium4+):
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))
上一篇
自動化測試會取代手工測試嗎?相關推薦