本文介紹了selenium 兩個 xpath 測試合二為一的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我嘗試結合檢查兩種情況:
I try to combine check for two scenarios:
如果啟動檢查失敗,我們會得到一個重試按鈕:
If startupcheck fails we get a try again button:
el = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.NAME, "Try again")))
或者 startupcheck 成功我們在一個自定義對象中得到一個 pin 輸入請求:
Or startupcheck succeed we get a pin enter request in a custom object:
el = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "http://Custom/Edit")))
如何將其合并為一項檢查而不必同時檢查兩者:我嘗試了以下方法:
How can this be combined into one check without having to check for both: I tried the following:
check = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, "http://Custom/Edit") or (By.NAME, "Try again")
))
但只檢查第一個 or
語句.
But only the first or
statement is checked.
推薦答案
您可以通過 lambda 表達式使用 OR
子句對兩個元素進行組合檢查,如下所示:
You can club up combine check for both the elements using OR
clause through a lambda expression as follows:
el = WebDriverWait(driver, 20).until(lambda x: (x.find_element_by_name("Try again"), x.find_element_by_xpath("//Custom/Edit")))
另一種解決方案是:
el = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.NAME,"Try again") and driver.find_element(By.XPATH,"//Custom/Edit"))
作為替代方案,您可以使用等效的 css-selectors 如下:
el = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='Try again'], Custom>Edit")))
參考文獻
- Python/Selenium:WebDriverWait 中的邏輯運算符預期條件
- 如何通過getText()從html內的多個子節點中提取動態文本
這篇關于selenium 兩個 xpath 測試合二為一的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!