Selenium 4 有哪些不一樣?
- 2022 年 8 月 21 日
- 筆記
- 自動化測試, Web UI 自動化測試
轉載請註明出處❤️
作者:測試蔡坨坨
原文鏈接:caituotuo.top/d59b986c.html
你好,我是測試蔡坨坨。
眾所周知,Selenium在2021年10月13號發布了Selenium4,目前最新的版本應該是Selenium 4.4.0。
以前一直用的Selenium3,那麼Selenium4相對Selenium3對我們做自動化測試來說有哪些需要注意的改進點或者變化呢?
今天,我們就來簡單地聊一聊Selenium4的那些新變化。
通過閱讀官方文檔,總結了幾個比較引人注目的變化點。
元素定位
在Selenium4中,不推薦把定位方式直接寫在方法名中,比如一系列的find_element_by_xx方法find_element_by_id、find_element_by_name、find_element_by_class_name等都被整合成為了一個方法find_element,並且通過By.method來選擇你的查找元素方法。
同理,多個元素定位推薦使用find_elements(By.method,"")。
注意:
-
雖然find_element_by_id、find_element_by_name……這些方法目前仍然可以使用,但是運行時會有DeprecationWarning警告


-
find_element(By.method, "xxx")這種方法在3版本也有,但是並沒有特彆強調
-
這種方法的使用需要引入類By,from selenium.webdriver.common.by import By
# author: 測試蔡坨坨
# datetime: 2022/8/20 19:19
# function: 元素定位
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("//www.baidu.com")
# 不推薦
# driver.find_element_by_id("kw").send_keys("測試蔡坨坨")
# driver.find_element_by_id("su").click()
# 推薦
driver.find_element(By.ID, "kw").send_keys("測試蔡坨坨")
driver.find_element(By.ID, "su").click()
time.sleep(3)
driver.quit()
不推薦:
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")
推薦:
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")
相對位置定位
在Selenium4中帶來了相對定位這個新功能,在以前的版本中被稱之為「好友定位(Friendly Locators)」,它可以通過將某些元素作為參考來定位其附近的元素。
find_element方法支援with(By)新方法,可返回RelativeLocator相對定位對象。
舉栗1:登錄功能,密碼輸入框在用戶名輸入框的下方
from selenium.webdriver.support.relative_locator import locate_with
username = driver.find_element(By.ID, "username")
password = driver.find_element(locate_with(By.ID, "password").below(username))
舉栗2:要獲取下圖所示所有文章標題左側的圖片地址
操作步驟:
- 獲取文章標題的位置作為錨點
- 通過with_tag_name查找元素的標籤,要找的是圖片標籤就是with_tag_name('img')
- 在文章標題的左側就是to_left_of(其他位置關係如:to_right_of、below、above、near、to_dict)
程式碼實現:
# author: 測試蔡坨坨
# datetime: 2022/8/20 19:19
# function: 相對位置定位
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import with_tag_name
driver = webdriver.Chrome()
driver.get("//www.caituotuo.top/")
driver.find_element(By.XPATH, '//*[@id="scroll-down"]/i').click()
time.sleep(2)
# 文章標題
article_title = driver.find_element(By.CLASS_NAME, "article-title")
# 獲取文章標題左側的圖片
elements = driver.find_elements(with_tag_name('img').to_left_of(article_title))
for e in elements:
print(e.get_attribute('src'))
driver.quit()
運行結果:

打開新的標籤頁或窗口
當我們需要測試打開幾個頁面或瀏覽器的場景時,在Selenium3中的操作步驟:
- 創建新的Web Driver實例
- 再使用Windowhandle方法中的Switch來執行操作
在Selenium4中有一個新的API,new_window,這意味著不需要再自己創建新的Web Driver實例
# author: 測試蔡坨坨
# datetime: 2022/8/20 23:37:32
# function: 打開新的標籤頁或窗口
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("//www.caituotuo.top/")
# 打開新的標籤頁,並切換進入
driver.switch_to.new_window("tab")
driver.get("//www.cnblogs.com/caituotuo")
print(driver.title)
# 打開新的窗口,並切換進入
driver.switch_to.new_window("window")
driver.get("//www.caituotuo.top/")
print(driver.title)
time.sleep(3)
driver.quit()

模擬移動設備
作用:將瀏覽器調成移動端模式,用於測試移動端H5頁面。
# author: 測試蔡坨坨
# datetime: 2022/8/20 21:11
# function: 模擬移動設備
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.execute_cdp_cmd(
"Emulation.setDeviceMetricsOverride",
{
"width": 400,
"height": 650,
"mobile": True,
"deviceScaleFactor": 100
}
)
driver.get("//www.caituotuo.top/")
driver.find_element(By.XPATH, '//*[@id="scroll-down"]/i').click()
time.sleep(3)
driver.quit()



