I'm trying to automate a process on this page, and after several attempts, I still have not figured out why the code below print True
when checking if the first_wallet_option
button is enabled, here:
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
first_wallet_option = driver.find_element(By.XPATH, "/html/body/div[1]/div/aside[2]/div[2]/div/div[2]/ul/li[1]/button")
print(first_wallet_option.is_enabled())
What happens is that after the page has been loaded, the program clicks the wallet icon located at the top right corner of this page, and according to its html source, those buttons are clearly disabled
at the moment it is done, as shown down below:
But, if I click twice the same wallet icon (one to close the aside element, and other to open it again), they get enabled:
So, I would like to learn how could I improve the code above to make sure it really detects when all of those 4 buttons (Metamask and the other 3) are enabled.
Ok, here it is:
First: The locators are not relative enough, so I took the freedom to refactor them.
Second: MetaMask is not a button as per DOM DOM Snapshot but the rest are. However, instead of relying on the buttons, I tried to rely on the li (which is common for all), and checking whether they are enabled or not.
Third: If you are trying to find if all the 4 elements are enabled, then you must use find_elements
instead of find_element
and then loop through the elements to check if each is enabled.
Please check if this works for you:
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")