I wrote a simple application in python, but when trying to click a button, I get an error Element not interactable. Here is a part of the code where it happens. I did wait for the element to render.
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '//*[#id="root"]/div[2]/div/div/div[2]/ul/li/button')))
driver.find_element(By.XPATH, '//*[#id="root"]/div[2]/div/div/div[2]/ul/li/button').click()
The error happens on this website: https://magiceden.io/ when clicking Connect Wallet in the top right corner and then trying to click Phantom in the opening window.
Reason behind this exception:
“element not interactabe” exception may occur due to various reason.
Element is not visible
Element is present in off screen (After scroll down it will display)
Element is present behind any other element
Element is disable
For your case it's locator, you can use below locator to click on it.
XPATH
//header/nav[1]/div[2]/div[2]/div[1]/button[2]
OR
//button[contains(#xpath,'1')]
OR
(//button[contains(.,'Connect Wallet')])[1]
The Syntax should be like
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))
element.click();
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I have tested with First and third XPATH, it's working for me, let me know if this does not solve your issue.
Related
I have been stuck on this issue, where no matter how i write out the navigation to the specific element, using .click() wont work, the element 100% loads in before the click is fired(this i know because i can manually click it), but it wont actually click, is there something about ng-click that needs specification or what, but hopefully i can atleast get some closure as to why this element wont be clicked
<div class="item-card-thumb-container" ng-click="root.onItemCardClick(userAsset)">
is there any possible way to click this element without specifically finding that element and adding .click() to it, because it doesn't seem to work, I understand its too vague, but also maybe the element isn't clickable unless the mouse hovers over the element, cause it is a UI
The desired element is a Angular element element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.item-card-thumb-container[ng-click^='onItemCardClick']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='item-card-thumb-container' and contains(#ng-click, 'onItemCardClick')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I've used Selenium for a number projects without problems. This one, however, no dice.
I'm trying to automate a check out process, and sometimes there is a prompt to re-input information. The window has a javascript popup (I think), and, while I can select the <div id="app"> portion without any problems, I CANNOT get find the iframe and focus on it.
So, this works:
driver.find_elements_by_id('app')
But, nothing works to recognize the frame and switch to it. For example, none these work:
driver.find_element_by_id()
driver.find_element_by_name()
driver.find_element_by_xpath()
And, I assume that because I can't find and switch to the iframe.
Help?
The element with the text as E-Mail Login is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.zoid-visible[title='ec_payment'][name^='__zoid__ec__payment']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='zoid-visible' and #title='ec_payment'][starts-with(#name, '__zoid__ec__payment')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
driver.switch_to.frame(iframe_elem)
or
driver.switch_to_frame(iframe_id)
in your case you can use :
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"));
to switch back to main use:
driver.switch_to_default_content()
Try this code to switch to frame
frame = driver.find_element_by_xpath('//div[starts-with(#id, "zoid-ec-payment")]/iframe')
driver.switch_to.frame(frame)
I cant click on a button on a webpage using selenium. Does anyone know how i can click it, I've tried every single method i can think of and i cant quite get it to work.
I've tried XPATH, ID's, searching for the text on the button, i've tried copy pasting code from other people.
This is the button i want to press:
<div id="sbWatchHeaderNext">
Next
</div>
This is the code I currently have that isn't working:
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//button[contains(text(),'Continue Watching')]")))
and
driver.find_element_by_xpath("//button[contains(text(),'Next')]").click()
Please if you have any suggestions i should try, let me know :)
With some methods i would get absolutely no errors in console but nothing would happen on the webpage, and with others i would get "Message: no such element: unable to locate element:..." and things like that in my console.
Seems you were pretty close. To click() on the desired element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Next"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#sbWatchHeaderNext>a[href^='https://pf.entertainow.com//f/p/enter?plid']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='sbWatchHeaderNext']/a[starts-with(#href, 'https://pf.entertainow.com//f/p/enter?plid') and text()='Next']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am pretty new to Python and Selenium, and I have got my script to do what I want so far, but I have this current coding from the website:
<a onclick="realPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent', ''); return false;" id="ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent" class="hub-content-item" actiontype="Secondary" href="javascript:__doPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent','')"><span>How to Project Wirelessly in Philly</span></a>
and I can't get it to click the link.
I've tried:
driver.find_element_by_text("How to Project Wirelessly in Philly")
and partial text
driver.find_element_by_id("ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent")
I've tried by tag but all returning errors. Looking up on here, I've seen stuff with Xpath but I have no clue how to do that, but if someone here does, then a little help with that, or any other simple code that hopefully allows me to click that link. (I know i will have to do .click() to eventually click it, but i cant even find the element yet)
The desired element is a dynamic element, so invoke click() on it you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.hub-content-item[id*='SearchExplorer'][actiontype='Secondary']>span")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='hub-content-item' and contains(#id,'SearchExplorer')][#actiontype='Secondary']/span[text()='How to Project Wirelessly in Philly']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am using selenium and python to automate web tasks. I have tried using multiple different functions to try to click on the button I need:
Reports
.find_element_by_link_text("Reports").click()
.find_element_by_id
.find_element_by_name
.find_element_by_class_name
.find_element_by_css_selector
Can't seem to make this work, any suggestions would be appreciated.
Using "find_elements_by_xpath". Right click and copy the XPATH from the browser.
Using explicit waits could potentially solve the problem. For explicit waits, you can either use the ID or XPATH. I prefer using XPATH. For getting the XPATH, right click on the element, click Inspect, right click on Reports and choose Copy, and then Copy XPATH. Now that we have the XPATH, do the following:
button_xpath = "the xpath of your element"
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, button_xpath))).click()
The above code will wait up to 10 seconds until the button element is found. If the element is not found, a TimeoutException will be given.
The following imports are necessary:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC