ElementClickInterceptedException: Message: element click intercepted: - python

I have code like this
for i in range(int(total_fill)):
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,f'//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2]')))
b = driver.find_element_by_xpath(f'//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2]').click()
but i got this error :
ElementClickInterceptedException: Message: element click intercepted: Element <div _ngcontent-oyq-c49="" class="btnBox">...</div> is not clickable at point (753, 26). Other element would receive the click: <span _ngcontent-oyq-c4="" class="sessionTimeHeading">...</span>
(Session info: chrome=91.0.4472.114)

First of all try using element_to_be_clickable instead of presence_of_element_located.
If this still doesn't help you can use JavaClick instead of driver.click().
So if this still will not work properly
for i in range(int(total_fill)):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,f'//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2]')))
b = driver.find_element_by_xpath(f'//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2]').click()
try this:
for i in range(int(total_fill)):
el = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,f'//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2]')))
driver.execute_script("arguments[0].click();", el)
I have to notice: it is strongly not recommended to use such a complex locators like //*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/div[{i+1}]/mat-card/div/div/div[4]/div[2] they are extremely not reliably.
Also, please read here about the ElementClickInterceptedException

Usually this happens when the wanted element is covered partially or completely by another element(a an alert message or some floating element).
It says so in the error message also: Other element would receive the click: <span _ngcontent-oyq-c4="" class="sessionTimeHeading">...</span>
So the first thing to do is make sure this element is either removed (by clicking the alert) or that the resolution of the screen is wide enough to be able to click the required element.

Related

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()

How to scroll down an element on page

I would like to scrape names of companies from https://justjoin.it/, however my scraper has a problem with page scrolling. I usually use the following method to scroll down page:
driver.execute_script("window.scrollTo(0, x)")
Unfortunately, on this page execute_script method doesn't work.
I tried also to manipulate the element with table od names by send_keys method, but my script raises error:
elem = driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[1]/div/div[2]/div[1]/div')
elem.send_keys(Keys.END)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Does anyone have an idea how to handle with this?
Try this
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
As you wrote, you first have to click on the element with the list of jobs offers and after that scroll to the desired element.
The scrolling will work fine in this case

Selenium element not interactable button (Python, Windows, Chromedriver)

error: raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=86.0.4240.75)
my code: driver.find_element_by_xpath('//button[contains(text(),"Buy Now")]').click()
html: https://i.stack.imgur.com/h0wGd.png
Since we do not have the full HTML page to find an accurate xpath and we have a small screenshot of the code, you could try to find the element using a different xpath technique.
//button[#class='button bits' and contains(., 'Buy Now')]
Then, you could get the element and click on it by using one of the following options. Option 1
driver.find_element(By.XPATH, "//button[#class='button bits' and contains(., 'Buy Now')]").click()
Option 2
driver.find_element(By.XPATH, "//button[#class='button bits' and contains(text(), 'Buy Now')]").click()
According to selenium documentation, that error occurs when:
"Thrown when an element is present in the DOM but interactions
with that element will hit another element do to paint order"
Make sure that your element is unique identified and use a wait. If is not enough, perform the click action via JavaScript.

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()

XPATH doesnt press a clickable button Python

my problem is that the first on is clicking and the second one doesnt and i dont undersatand why
WebDriverWait(rootdiv, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[#onclick[contains(.,JTC)]]")))
rootdiv.find_element_by_xpath("//li[#onclick[contains(.,'JTC')]]").click()
WebDriverWait(depdiv, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[#onclick[contains(.,AJU)]]")))
depdiv.find_element_by_xpath("//li[#onclick[contains(.,'AJU')]]").click()
depdiv and root div are children to look under for the certain li cause the root changes from the first to the second..
i've checked that the div is visible and in the first one, its clicking, the second time it cant find the object and im getting a time error
part of the code im trying to fed from..
<div class = "divCombo4">
<ul><li>....</li><ul>
<ul><li>...</li><ul>
</div>
and my depdiv is rooting to the find_element_by_id("divCombo4")
one of those ul li contains
onclick="selecionou('AJU', this,'.txtBusca4', 'false', 'destino', 'Estouem2', 'AJU');"
First of all, you need a dot to make the expressions context-specific. Also, the wait.until() returns a WebElement instance and you can use it instead of issuing a "find element" command again:
WebDriverWait(rootdiv, 10).until(EC.element_to_be_clickable((By.XPATH, ".//li[#onclick[contains(.,JTC)]]"))).click()
WebDriverWait(depdiv, 10).until(EC.element_to_be_clickable((By.XPATH, ".//li[#onclick[contains(.,AJU)]]"))).click()

Categories