Python Selenium: Element Is Not Clickable At Point - python

I am using the code below to click on the highlighted tab that you see on the attached screenshot with the corresponding HTML code. The tab is always visible on the bottom of the script so there is no need to scroll down to it.
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))
element.click()
The error that I get is below. What am I doing wrong?
Message: unknown error: Element <a class="tab-button has-icon icon-only" href="#" role="tab" ng-
reflect-tab="[object Object]" id="tab-button-settings" aria-controls="tabpanel-t0-1" aria-
selected="false">...</a> is not clickable at point (1440, 1017). Other element would receive the
click: <div class="click-block click-block-enabled click-block-active"></div>
(Session info: chrome=78.0.3904.108)

you can use action class to avoid above issue
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings"))
ActionChains(driver).move_to_element(element).click().perform()

Another way alternative to javascript is to use touch actions
from selenium.webdriver.common.touch_actions import TouchActions
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))
touchactions = TouchActions(driver)
touchactions.tap(element).perform()

The desired element is an Angular element so to locate and click() on the element you have 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, "a.tab-button.has-icon.icon-only#tab-button-settings"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='tab-button has-icon icon-only' and #id='tab-button-settings']"))).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
In case the unknown error still occurs you may have to induce WebDriverWait additionally for the invisibility_of_element_located() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.click-block.click-block-enabled.click-block-active")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.tab-button.has-icon.icon-only#tab-button-settings"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[#class='click-block click-block-enabled click-block-active']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='tab-button has-icon icon-only' and #id='tab-button-settings']"))).click()
References
You can find a couple of relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

Related

How to click on this button element using Selenium and Python

Here is the full code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(executable_path = r"C:\Users\H\Desktop\Automated_Tasks\msedgedriver.exe") # Modify the path here...
# Navigate to URL
driver.get("https://powerusers.microsoft.com/t5/notificationfeed/page")
#accept cookies
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/center/div[1]/div/div[2]/button[1]"))).click()
#click button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).click()
The following line:
button=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "viewMoreLink"))).click()
gives an error:
File C:\Program Files\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:249 in check_response
raise exception_class(message, screen, stacktrace)
ElementClickInterceptedException: element click intercepted: Element is not clickable at point (476, 1865)
(Session info: MicrosoftEdge=109.0.1518.61)
Here is the HTML for the button I'm trying to click.
<a onclick="return LITHIUM.EarlyEventCapture(this, 'click', true)" class="lia-button lia-button-primary view-more-icon lia-link-ticket-post-action" data-lia-action-token="gY01pJ4IhqNcqA8Ouq1d20HgZFI9CVTHrEYgxObqxWantjAxFsOxTacdu8LdHjd0" rel="nofollow" id="viewMoreLink" href="https://powerusers.microsoft.com/t5/notificationfeed/page.notificationlist.notificationlistitems.viewmorelink:viewmore/notificationfeed.lastLoadedTimestamp/1674422253100/notificationfeed.lastViewedTimestamp/1674505573710/container_element/notificationList"><span>Show more</span></a>
Here is a picture showing where the desired element is in:
As per the given HTML to click on the element with text as Show more 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, "a.lia-button.lia-button-primary.view-more-icon.lia-link-ticket-post-action#viewMoreLink[href$='notificationList'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).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
You can find a relevant detailed discussion in ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python

Selenium/Python: no such element with: driver.find_element(By.ID)

I try to locate elements on a website which are in a form to then send keys to login but I get an error, see below:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}
Line of code is:
driver.find_element(By.ID, "username").send_keys("test123")
For HTML code see:
HTML code
I use latest version of Python/Selenium and PyCharm.
I hope someone can help.
I don't see anything wrong in that line of code. Try one of the below
Try applying implicit wait as below:
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(20)
Check if there is a frame/ iframe covering this element, if yes, try below code before locating the element
iframe = driver.find_element_by_xpath("<XPath expression of the frame>")
driver.switch_to.frame(iframe)
To switch back to the main DOM:
driver.switch_to.default_content()
If the frame doesn't exist, then try inducing explicit wait as below:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
Below imports you will require:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Given the HTML:
the element is a <input> element which would accept text input using send_keys() method.
Solution
Ideally to send a character sequence to an <input> 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username[name='username']"))).send_keys("Coding_89")
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='input' and #id='username'][#name='username']"))).send_keys("Coding_89")
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 Error: Element not clickable because another element obscures it

I want to click on a button that is either not yet clickable or has another element on top of it.
But as soon as it is clickable, I want to press it.
I tried this:
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "likeClick")))
button.click()
But it doesnt work because I still get the error
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="likeClick"> is not clickable at point (400,553) because another element <div class="delete-overlay"> obscures it```
This error message...
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="likeClick"> is not clickable at point (400,553) because another element <div class="delete-overlay"> obscures it
...implies that the click to the <a> element failed as the element was obscured the <div> element which seems to be an temporary overlay .
Solution
To click on the desired element you need to induce WebDriverWait for the invisibility_of_element_located() of the overlay and then induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.delete-overlay")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.likeClick"))).click()
Using XPATH:
WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.XPATH, "//div[#class='delete-overlay']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='likeClick']"))).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

How to click pop up window button within a iframe using Selenium with Pyhton

I want to click the "Akkoord" button but I am unable to do that. I already tried different methods but they are not working. Any help will be appreciated.
one of the codes I tried.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.demorgen.be/nieuws')
time.sleep(20)
driver.find_elements_by_class_name('message-component message-button no-children pg-accept-button')[0].click()
The element Akkoord is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.demorgen.be/nieuws")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Akkoord']"))).click()
Using XPATH:
driver.get("https://www.demorgen.be/nieuws")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'sp_message_iframe')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Akkoord']"))).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
Browser Snapshot:
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
use xpath or css
css :
[class="message-component message-button no-children pg-accept-button"]
driver.find_elements_by_css_selector('[class="message-component message-button no-children pg-accept-button"]')[0].click()
xpath:
//*[#class="message-component message-button no-children pg-accept-button"]
driver.find_elements_by_xpath('//*[#class="message-component message-button no-children pg-accept-button"]')[0].click()
find_elements_by_class_name expects single class name as argument thats why its not working as space in class indicates multiple classes.
THe find by class actually uses css under the hood. So if you want to find element having multiple class . You can replace space with '.' (THis works only in python)
driver.find_elements_by_class_name('message-component.message-button.no-children.pg-accept-button')[0].click()
Also
The element is inside iframe
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
frame = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#sp_message_container_404503 iframe")))
driver.switch_to_frame(frame)
driver.find_element_by_css_selector(
'[class="message-component message-button no-children pg-accept-button"]').click()

Message: element click intercepted: Element <span>...</span> is not clickable at point (657, 594). Other element would receive the click with Selenium

I was making a webscraper to get gpu stocks from https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us to get my hands on a 30 series card, I am using python with bs4 and selenium for this.
I want to load more shopping items, on the website it has this load more button. So I grabbed its class and made it so selenium clicks it:
driver.find_element_by_class_name("buy-link").click()
but it says that the element in non interactiable, HTML for the button
The exact error it gives me is:
Message: element click intercepted: Element <span class="extra_style buy-link" data-color="#76b900" data-secondary-color="#fff" style="visibility: visible; cursor: pointer;" data-mpn-code="NVGFT070">...</span> is not clickable at point (657, 594). Other element would receive the click: <div class="popBg" id="nv-buy-now-model" style="display: block;">...</div>
I don't know much HTML, how can I achieve clicking this button
The LOAD MORE element is a Angular element so to click on it you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#cookiePolicy-btn-close>span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
Using XPATH:
driver.get("https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='cookiePolicy-btn-close']/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='buy-link load-more-btn' and #value='LOAD MORE']"))).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
Browser Snapshot:
The problem is that there are some 20 elements on that page that match your first locator, .find_element_by_class_name("buy-link"). In this case, if you make your locator more specific, you can isolate it down to just the "LOAD MORE" INPUT button.
Try this instead
driver.find_element_by_css_selector("input.buy-link").click()
It's generally a good practice to add a wait unless you are sure you won't ever need it.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='CLOSE']")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link")).click()
See the docs for more info and details.

Categories