我正在嘗試自動化
但是,如果我單擊兩次相同的錢包圖示(一個關閉旁邊的元素,另一個再次打開它),它們就會被啟用:

所以,我想了解如何改進上面的代碼,以確保它真正檢測到所有這 4 個按鈕(Metamask 和其他 3 個)何時都啟用。
uj5u.com熱心網友回復:
好的,這里是: 首先:定位器不夠相關,所以我自由地重構了它們。第二:根據 DOM DOM Snapshot , MetaMask 不是一個按鈕,但其余的都是。但是,我沒有依賴按鈕,而是嘗試依賴 li (這對所有人來說都很常見),并檢查它們是否啟用。第三:如果您試圖查找是否所有 4 個元素都已啟用,那么您必須使用find_elements而不是find_element然后回圈遍歷元素以檢查每個元素是否已啟用。請檢查這是否適合您:
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, "//*[@title='Wallet']"))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, "//*[@title='Wallet']")
wallet_button.click() #click that wallet button
wallet_options = driver.find_elements(By.XPATH, "//*[@data-testid='WalletSidebar--body']//li")
for wallet in wallet_options:
if wallet.is_enabled():
print(f"{wallet.text} is enabled")
else:
print(f"f{wallet.text} is not enabled")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418975.html
標籤:
上一篇:為什么在我使用time.sleep()之前Selenium在頁面上找不到任何元素?
下一篇:selenium.common.exceptions.NoSuchElementException使用Selenium和Python將文本發送到輸入欄位時出錯
