'ElementNotVisibleException' Unable to click button with Selenium - python

A rather 'tricky' question that makes my head spin. I'm unable to click a button while using Selenium.
Css selector property: ValidateOrderForm > span > button
Xpath: #//*[#id="ValidateOrderForm"]/span/button
Attempts:
d.find_elements_by_xpath('ValidateOrderForm')[1].clear()
d.find_elements_by_id('ValidateOrderForm')[0].click()
d.find_element_by_css_selector("button.call-to-action").click()
And really everything in between.
Update: it gives the following error
ElementNotVisibleException: element not interactable

Related

I'm unable to locate the element to perform actions on it

I'm facing an issue locating the element on screen when there are no unique identifiers like ID, text etc. As it opens URL, I need to scroll down and click on button - 'Get Started' to proceed!...
Below is my code:
global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://My URL")
driver.implicitly_wait(10)
screen = driver.find_element(By.XPATH, '//div[#class="swiper-wrapper"]')
screen.click() (- This step doesnt through any error, i tried using this to scroll down the screen)
element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]')
driver.execute_script("arguments[0].scrollIntoView(true);", element )
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(),"Get Started")]'))).click()
or
element.click()
Please help me in determining how to locate the element.
enter image description here
In this case you are trying to find span which is inside #shadow-root, Selenium won't be able to click elements inside #shadow-root in easy way. Look here: How to click button inside #shadow-root (closed) using Selenium and Python
But in your case you probably don't need/want to click this specific span, becouse you have custom element ion-button, which has some click event listener attached to it.
You can check XPATH value in browser devtools. If you go to Elements tab, you can right click desired element and choose option Copy>Copy Xpath. Also make sure your element is not inside iframe tag, it could cause problem as well.

Selenium unable to find Button

I am trying to click on the "Next Page"-Button on the Web of Science Search-Site to iterate through all pages.
Here is a screenshot of the HTML of the page (highlighted is the button)
This is my code to find the button:
driver.find_element_by_class_name('mat-focus-indicator mat-icon-button mat-button-base').click()
But I receive this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mat-focus-indicator mat-icon-button mat-button-base"}
I have tried so many ways of identifying the button (find_by_id, find_by_name, find_by_link_text) but nothing works.
What am I doing wrong?
Thank you in advance
Maybe try with Query Selector like:
driver.find_element_by_css_selector('button[data-ta="next-page-button"]')
You can always try selectors on elemnts panel (like on screenshort) and type your selector in field "Find by string, css, or xpath"
Class name do not have support for spaces, remove spaces and put . to make it css selector :
driver.find_element_by_css_selector('button.mat-focus-indicator.mat-icon-button.mat-button-base').click()
also try to put some wait before this to make it more consistent.

Selenium two buttons with same xpath. Unable to click the one I need

There are two "EXPORT" buttons on the web page I am testing.
I am able to click the first using:
driver.find_element_by_xpath("//button[contains(.//text(),'EXPORT')]").click()
the driver then choses a dropdown and another div appears, again with an EXPORT button.
My code error when trying to execute:
driver.find_element_by_xpath("//button[contains(.//text(),'EXPORT')]").click()
for this element is:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1824, 89). Other element would receive the click: ...
(Session info: chrome=92.0.4515.107)
So it says it is unable to click the first EXPORT button and the second one would get the click.
I've tried modifying my xpath expression to:
driver.find_element_by_xpath("//button[class()='MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary' and (.//text(),'EXPORT')]").click()
and a bunch of other attempts, but each time it is unable to locate. I want to be able to click the second EXPORT button (in the button class snippet shown in the pic).
You can differentiate between these two buttons with the help of xpath indexing.
Sample below :-
(//span[text()='EXPORT']/..)[1]
and for second button :-
(//span[text()='EXPORT']/..)[2]
and for this error
selenium.common.exceptions.ElementClickInterceptedException: Message:
element click intercepted: Element ... is not clickable at point
(1824, 89). Other element would receive the click: ... (Session info:
chrome=92.0.4515.107)
I would suggest you to open browser in full screen mode when it's launched.
driver.maximize_window()
or probably, you can use ActionChain as well :-
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.find_element(By.XPATH, "(//span[text()='EXPORT']/..)[2]")).click().perform()
Update 1 :-
You can use find_elements as well to store all the web element and then try to click the one you want.
all_export_button = driver.find_elements(By.XPATH, "//span[text()='EXPORT']/..")
#to click on first
all_export_button[0].click()
#to click on Second
all_export_button[1].click()

Selenium - Visibility of element in HTML page (Python)

I'm trying to scrape the following URL: https://www.vivareal.com.br/venda/sp/holambra/imovel-comercial_comercial/. It has two pages only. However, there is a button in the bottom which I can locate via XPATH: "//*[contains(#title, "Página anterior")]".
I'm trying to set a variable for when the button is not clickable, like:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[contains(#title, "Página anterior")]')))
But it returns an element, even if the element is not clickable (I'm testing manually too). Is there any sugestions? Thanks!
When the button is not clickable, it has an attribute called data-disabled; otherwise that attribute is missing. The attribute has no value, but if you use the following CSS Selector it should grab that element only if there is no data-disabled attribute:
driver.find_element_by_css_selector('a[title="Página anterior"]:not([data-disabled]')

How to fix NoSuchElementException Python error on clicking on webpage button?

Basically I need to click on a button, which is inside a frame on a webpage. I have tried:
1) Switching to frame, which works fine, doesn't return any errors:
driver.switch_to.frame(driver.find_element_by_css_selector('#iframe'))
3) Adding time delay of 20 seconds, which doesn't change the result as it just times out in the end:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "dx-button"))).click()
I believe the CSS element's name is correct as I have copied it using Developer Mode -> Copy Selector.
Is there anything else that I can do in order for selenium to find this CSS element?
Error before adding time delay:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"dx-button"}
Error after adding time delay:
selenium.common.exceptions.TimeoutException: Message:
Thank you.
May be check if the switched iframe is the accurate one that has the button.
If that's correct, Can you try the same line with XPATH locators?
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(#class, 'button')]"))).click()

Categories