Find button with Selenium on python - python

I'm trying to retrieve data from this button but it raises the following error: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable.
Button source code:
<button class="Blockreact__Block-sc-1xf18x6-0 Buttonreact__StyledButton-sc-glfma3-0 bqHBns jFqMrE ActionButtonreact__StyledButton-sc-7jpoey-0" type="button"><div class="Blockreact__Block-sc-1xf18x6-0 Flexreact__Flex-sc-1twd32i-0 olSpy jYqxGr"><i value="add" size="24" class="Iconreact__Icon-sc-1gugx8q-0 irnoQt material-icons">add</i></div></button>
My code:
browser.find_element_by_xpath("//button[#type='button']")
Page: Add item on https://opensea.io/asset/create

Since the browser maximize isn't working for you.
Did you try with ActionsChains ?
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(browser.find_element_by_xpath("//i[#value='add']/../..")).click().perform()

Click the button not the icon. xpath:
.//body/div[1]/div[1]/main/div/div/section/div[2]/form/section[6]/div[1]/div/div[2]/button
You can also use element waits package to ensure the button is interactable first
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
wait.until(ExpectedConditions.elementToBeClickable(by));
Then click

Related

How to use click() function of selenium in case of "button" attribute in HTML?

I want to use the click() function of selenium on these buttons in the following manner(the first two clicks):
For reference the 1st button I want to click and its code:
2nd button with its code:
I tried using
driver.find_element(By.XPATH,"//button[contains(#type,\"button\")]").click()
on the first button (the "X" mark) but it didn't work and got this error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="button" class="el-button yellowBtn el-button--default">...</button> is not clickable at point (606, 507). Other element would receive the click: <img src="https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png" alt="" style="width: 100%;">
If I somehow get it to click the first button then getting it to click the second button will be much easier.
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted
Above exception implies that the element you are trying to action upon isn't clickable, as some other element is overshadowing it.
Try by inducing wait(see below):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"Your XPath expression here"))).click()
if the above doesn't work, try using execute_script() method(see below):
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "Your XPath expression here"))))
Required imports statements:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I think you should wait until the element is visible in the modal dialog box. U can use both the Explicit and Implicit waits methods for the above problem.
Always read exception and/or error messages carefully to see what you can learn about the problem. In this case, it tells you exactly what happened. I reformatted and shortened it to make it more readable.
ElementClickInterceptedException:
Message: element click intercepted: Element <button...> is not clickable.
Other element would receive the click: <img...>
Selenium is telling you that it tried to click the BUTTON but the click was intercepted (it never got clicked because there was another element covering it). The element that got clicked instead was an IMG.
Without seeing the full HTML of the page it's hard to say but your BUTTON locator looks pretty generic and there's a good chance that you clicked a different BUTTON than you intended. I would use the locator below instead to click on the I tag.
wait = WebDriverWait(driver, 10)
close_icon = (By.CSS_SELECTOR, "i.el-dialog__close")
wait.until(EC.element_to_be_clickable(close_icon)).click() # click the close icon
wait.until(EC.invisibility_of_element_located(close_icon)) # wait for the close icon to disappear indicating the dialog is fully closed
# continue script
If that still doesn't work, you will probably need to use a JS click. If you are writing UI tests, then you should avoid JS clicks unless no other solution can be found because it allows the script to do things that a user can't. If you aren't writing tests, then it likely doesn't matter.
wait = WebDriverWait(driver, 10)
close_icon = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.el-dialog__close")))
driver.execute_script("arguments[0].click()", close_icon)
# continue script
You desired element is:
But your line of code:
driver.find_element(By.XPATH,"//button[contains(#type,\"button\")]").click()
is attempting to click on:
<button type="button" class="el-button yellowBtn el-button--default">...</button>
which isn't the desired element and still the click attempt is intercepted by:
<img src="https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png" alt="" style="width: 100%;">
Solution
To click 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, "button.el-dialog__headerbtn[aria-label='Close'] > i.el-dialog__close.el-icon.el-icon-close"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='el-dialog__headerbtn' and #aria-label='Close']/i[#class='el-dialog__close el-icon el-icon-close']"))).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

SELENIUM PYTHON ElementClickInterceptedException:

I'm writing a Selenium Python script. Unfortunately, I can't click "Sign in" button or check mark.
I've tried many ways. I'll be glad if anyone helps me plz.
Link:
https://stagingskateontario.memberlounge.ca/auth/login
My Code:
wait.until(EC.element_to_be_clickable((By.ID, ":r2:")))
driver.find_element(By.ID,":r2:").click()
Error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (790, 723)
(Session info: chrome=103.0.5060.134)
enter image description here
I was able to click the button with javascript
element = browser.find_element_by_xpath("/html/body/div/main/div/div/div[2]/form/button")
browser.execute_script("arguments[0].click();", element)
JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. JavaScriptExecutor provides two methods “executescript” & “executeAsyncScript” to run javascript on the selected window or current page.

How can I click the button to see more values using Selenium Python?

from selenium import webdriver
import time
import datetime
driver = webdriver.Chrome(
executable_path=r'C:\Users\Kashi\Downloads\Compressed\chromedriver_win32/chromedriver.exe')
driver.get('https://www.mql5.com/en/quotes/currencies')
driver.find_element_by_xpath('//*[#id="list-view-btn"]').click()
time.sleep(2)
driver.find_element_by_xpath('//*[#id="symbols_more"]').click()
I'm unable to click on the button "More Symbols". I've tried the upper method but I can't. Please help me to click on the button "More Symbols", as I'm able to click on the list view button but I'm getting an error on the 2nd one.
ElementClickInterceptedException: Message: element click intercepted: Element <a id="symbols_more" class="button button_green" href="#" onclick="return Navigator.Overview.loadMore(this, 1);">...</a> is not clickable at point (644, 550). Other element would receive the click: ...
You are not handling the cookies which is obscuring your target element.
#Close the Cookies element
driver.find_element_by_xpath("//div[#id='floatVerticalPanel']/span").click()
driver.find_element_by_xpath("//*[#id='list-view-btn']").click()
#Add a wait time for the page to load
driver.find_element_by_xpath("//a[#id='symbols_more']").click()

python selenium: element click intercepted:

I'm trying to click via find_element_by_xpath() as i did it always. In the case below, there is an error when try to click this element. I'm quite new with selenium and researched already. It seems that i have to click on specific coordination. Do you have any idea how to solve this problem? i'm struggling for quite a while.
section of HTML code:
<map ng-if="!enableSvgHighlighting" id="clickareasVillage" name="clickareasVillage"
ng-show="areas.village.length > 0" class="">
<area ng-repeat="a in areas.village" location-id="32" on-pointer-over="highlightStart(32)" on-
pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)" tg-
coords="758,341,758,342,761,343,763,344,767,345,769"
coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32" class="clickable">
my Code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
ele1=driver.find_element_by_xpath("//map[#id='clickareasVillage']/area[2]")
ele1.click()
error message:
ElementClickInterceptedException: element click intercepted: Element
<area ng-repeat="a in areas.village" location-id="32"
on-pointer-over="highlightStart(32)" on-pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)"
tg-coords="758,341,758,342,761,343,763,344,767,345,769" coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32"
class="clickable"> is not clickable at point (391, 477).
Other element would receive the click: <img ng-if="!enableSvgHighlighting"
ng-show="areas.village.length > 0" class="clickareas" src="layout/images/x.gif" usemap="#clickareasVillage" data-cmp-info="9">
on other treads at stackoverflow, people suggest to use the following line. If i try, there is a TimeoutExeption.
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
As said above, usually when you encounter this error it's because a pop-up or an invisible overlay is preventing the driver from clicking on your element.
You said that JS clicking wasn't working either so i'm going to give you 2 quick workarounds that you can try:
ActionChains to move and click on your element:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).click().perform()
Send keys using ActionChains
from selenium.webdriver.common.action_chains import ActionChains
for i in range(#determine how much time):
ActionChains(driver).send_keys(Keys.TAB).perform() #tab until element is selected
ActionChains(driver).send_keys(Keys.ENTER).perform() #press enter to "click" on it
Let me know if that helped you.
Try using javascript
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
driver.execute_script("arguments[0].click();", element)
There may be something which could be preventing click on the element, check if any pop-up occuring.

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

I am currently working on a project which fills a form automatically. And the next button appears when the form is filled, that's why it gives me an error.
I have tried:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//input[#type='button' and #class='button']")))
Next = driver.find_element_by_xpath("//input[#type='button' and #class='button']")
Next.click()
HTML:
<span class="btn">
<input type="button" value="Next" class="button" payoneer="Button" data-controltovalidate="PersonalDetails" data-onfieldsvalidation="ToggleNextButton" data-onclick="UpdateServerWithCurrentSection();" id="PersonalDetailsButton">
</input>
<div class="clearfix"></div>
</span>
ERROR:
selenium.common.exceptions.ElementClickInterceptedException: Message:
element click intercepted: Element is not clickable at point (203, 530).
Other element would receive the click: ... (Session info: chrome=76.0.3809.132)
If the path of the xpath is right, maybe you can try this method to solve this problem. Replace the old code with the following code:
button = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].click();", button)
I solved this problem before, but to be honestly, I don't know the reason.
This error message...
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (203, 530). Other element would receive the click: ... (Session info: chrome=76.0.3809.132)
...implies that the click() on the desired element was intercepted by some other element and the desired element wasn't clickable.
There are a couple of things which you need to consider as follows:
While using Selenium for automation using time.sleep(secs) without any specific condition to achieve defeats the purpose of automation and should be avoided at any cost. As per the documentation:
time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
You can find a detailed discussion in How to sleep webdriver in python for milliseconds
As WebDriverWait returns the WebElement you can invoke the click() method directly.
Solution
To click on the button with value as Next you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#PersonalDetailsButton[data-controltovalidate='PersonalDetails']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #id='PersonalDetailsButton'][#data-controltovalidate='PersonalDetails']"))).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 faced similar issues, the .click() always returns a Not clickable exception. the
driver.execute_script('arguments[0].click()', button)
does the magic. You can also use it to execute any other js script this way
script = 'your JavaScript goes here'
element = driver.find_element_by_*('your element identifier goes here')
driver.execute_script(script, element)
I looked at the exact element that was causing it and it was a banner about consent/cookies. So at first, I made sure it clicked "OK" on the consent banner and then I clicked the other button that I needed. Hope it helps someone.
It look's like there are some other elements which are having the same xpath try changing the xpath something like this
Next = driver.find_element_by_xpath("//input[#id='PersonalDetailsButton']");
Next.Click();
or
Next = driver.find_element_by_xpath(//input[#value='Next' and #id='PersonalDetailsButton']);
Next.Click();
Try first xpath if that doesn't work go with the second one . If that also doesn't work try using sikuli. I am pretty sure that first xpath will work
I faced a similar issue and I observed something that might help to understand the root cause of the issue. In my case, I was able to click at an element being in PC view mode of the website but failed to do so in mobile view (in which I needed my script to run). I found out that in mobile view, ordering of elements (li in my case) changed in view while they remained same in the html document. That's why I was not able to click on it without actually scrolling to it first. It might also explain why this works: -
driver.execute_script("arguments[0].click();", button)
I don't have enough rep to comment but the common reason for this error might be Selenium locates the element from DOM on screen and locate the x-y coordinates (300, 650) then clicks on them but if some changes takes place on screen in between the click duration, for example google ads or some pop-up then it's unable to click on it resulting in this exception
I'm just guessing if anyone has a proper explanation to pls share
I had the same problem too. But my problem was not with the element. The button was activated with href. I changed the code from
<a class="services-button" href="desired url">
To
<a class="services-button" onclick="location.href='{% url "desired url" %}'";">
This solution worked for me :
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox(executable_path="")
driver.get("https://UrlToOpen")
action = ActionChains(driver)
firstLevelMenu = driver.find_element_by_id("menu")
firstLevelMenu.click()
source : http://allselenium.info/mouse-over-actions-using-python-selenium-webdriver/
"selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable ... "
This exception occurs when element is not found on a web page (When the element we are looking for is at bottom part of the page which is not loaded yet)
So we you can scroll the page using javascript and load complete page and
from selenium.webdriver.common.by import By
from selenium import webdriver
url = "YOUR URL"
SCROLL_PAUSE_TIME = 0.5
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
def scroll_page():
i = 0
while i < 5:
# Scroll down to 500 pixel
driver.execute_script("window.scrollBy(0, 500)", "")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Will scroll only for 4 increments of 500px
i += 1
You could try:
driver.execute_script("arguments[0].click();", button)
This solution solved my problems when I faced similar issues.

Categories

Resources