在我的網頁上有兩種不同型別的錯誤。
第一個是:
<div class="warning" data-text="text-field-error" dir="ltr">Diese ID ist nicht verfügbar.</div>
第二個是:
<div class="separator-notice text-notice text-margin theme-noticeerror-font" dir="ltr">Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten.</div>
例如,如果我嘗試了錯誤的電子郵件,它沒有具體的值,你可以重復多長時間。時不時地不一樣。現在第一個錯誤意味著此電子郵件(在我的情況下為 ID)無效。
第二個錯誤的含義是服務器連接失敗。現在使用 Selenium,我想處理這兩個不同的錯誤,如下所示:
for line in lines:
driver.find_element_by_id(input_id).send_keys(line)
driver.find_element_by_class_name(check_available).click()
count = 1
time.sleep(1)
try:
# Check if I get error one
except:
# I got an error two
else:
pass
我已經環顧四周,但找不到任何符合我要求的東西。我也用 xpath 嘗試過這樣的文本:
try:
driver.find_element_by_xpath("//div[contains(text(), ' Diese ID ist nicht verfügbar.')]")
except:
# It has to be error two
所以我的問題是:如何檢查我當前遇到的錯誤以及如何處理該錯誤。例如
if error1:
print("error_one")
if error2:
print("error_two")
uj5u.com熱心網友回復:
即使沒有try-你也可以做到這一點except。
你可以這樣做:
for line in lines:
driver.find_element_by_id(input_id).send_keys(line)
driver.find_element_by_class_name(check_available).click()
count = 1
time.sleep(1)
first = driver.find_elements_by_xpath("//div[contains(text(), 'Diese ID ist nicht verfügbar.')]")
second = driver.find_elements_by_xpath("//div[contains(text(), 'Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten')]")
if first:
#you have got the first notification
if second:
#you have got the second notification
您也可以使用預期條件和try-來執行此操作except,但這種方法看起來是最簡單的。
uj5u.com熱心網友回復:
要探測這兩個錯誤,您需要將驗證包裝在try-except{}塊中,從而為visibility_of_element_located()引入WebDriverWait,您可以使用以下Locator Strategies:
for line in lines:
driver.find_element_by_id(input_id).send_keys(line)
driver.find_element_by_class_name(check_available).click()
count = 1
time.sleep(1)
try:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(., 'Diese ID ist nicht verfügbar.')]")))
except TimeoutException:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(,, 'Bei der Verbindung zum Server ist eine Zeitüberschreitung aufgetreten.')]")))
注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409741.html
標籤:
下一篇:如何解密使用XOR加密的字串
