我想使用 ID 查找元素,但似乎網路有時會更改 ID。
有2個ID,第一個ID可以讓我直接輸入郵箱,但是如果出現第二個ID,我需要先點擊一個按鈕才能輸入我的郵箱。我試過使用if和else邏輯,但它顯示了一些錯誤。這是我的代碼:
@pytest.mark.parametrize('a,b', key0)
def test_login_successful(self, a,b):
time.sleep(5)
if WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'login-find-account-form'))) == True:
pass
else:
# WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'login-form')))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="login-form"]/div[1]/div/div/div/a'))).click()
email_box = '//*[@id="user_name"]'
email_input = driver.find_element_by_xpath(email_box)
email_input.send_keys(a)
submit = driver.find_element_by_xpath('//*[@id="submit_button"]').click()
如果您有任何想法可以這樣做,我將不勝感激。
謝謝
uj5u.com熱心網友回復:
您可以改用CSS選擇器,如下所示:
ID_1 = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS, '#login-form,#login-find-account-form')))
逗號用作ORCSS的運算子,如果它沒有找到第一個,那么它將嘗試找到第二個。重要的是您要知道這個選擇器不能作為AND它不會回傳兩個結果的意思。
編輯:
有一種簡單的方法可以實作這一點(不是正確的方法)。
實作您所需要的簡單方法:
@pytest.mark.parametrize('a,b', key0)
def test_login_successful(self, a,b):
time.sleep(5)
# The element to be found on the second condition
element_of_interest = driver.find_elements_by_xpath('//*[@id="login-form"]/div[1]/div/div/div/a')
# element_of_interest is a list, if is empty this condition
# is not valid and then it won't attempt to click on it
if element_of_interest:
element_of_interest[0].click()
email_box = '//*[@id="user_name"]'
email_input = driver.find_element_by_xpath(email_box)
email_input.send_keys(a)
submit = driver.find_element_by_xpath('//*[@id="submit_button"]').click()
對這里的一些評論。看起來你真的不需要第一個條件,因為你沒有在那里做任何事情。因此我只添加了第二個條件。
如果您查看解決方案,您會發現使用find_elements_by_xpath(復數形式)而不是這樣做find_element_by_xpath的原因是因為如果未找到該元素,find_elments_by_xpath則將回傳一個空串列。
如果您需要等待 10 秒直到出現這些元素之一,那么您可以執行以下操作:
@pytest.mark.parametrize('a,b', key0)
def test_login_successful(self, a,b):
time.sleep(5)
# The element to be found on the second condition
element1 = driver.find_elements_by_id('login-find-account-form')
element2 = driver.find_elements_by_xpath('//*[@id="login-form"]/div[1]/div/div/div/a')
# Waits up to 10 seconds to find either element 1 or 2
retries = 10
while not element1 or not element2:
time.sleep(1)
retries -= 1
if retries < 0:
raise Exception("some exception in here")
element1 = driver.find_elements_by_id('login-find-account-form')
element2 = driver.find_elements_by_xpath('//*[@id="login-form"]/div[1]/div/div/div/a')
# element2 is a list, if is empty this condition
# is not valid and then it won't attempt to click on it
if element2:
element2[0].click()
email_box = '//*[@id="user_name"]'
email_input = driver.find_element_by_xpath(email_box)
email_input.send_keys(a)
submit = driver.find_element_by_xpath('//*[@id="submit_button"]').click()
對此有一個更好的實作,我建議查看硒的頁面物件模型檔案,您可以從該設計模式中受益匪淺:https : //www.selenium.dev/documentation/guidelines/page_object_models/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376126.html
標籤:Python 硒 硒网络驱动程序 网络驱动程序 pytest
上一篇:無法點擊“主頁”按鈕Python
