Selenium: element is not clickable because an other element obscures it - python

I've been searching around the site but can not seem to find a fix that works for me. I am trying to click a button that should load more reviews.
driver.get("https://www.mediamarkt.nl/nl/product/_philips-essential-airfryer-xl-hd9260-90-1653991.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".bv-content-btn-pages-load-more"))).click()
Is the code I am using. This results in an error:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <button class="bv-content-btn bv-content-btn-pages bv-content-btn-pages-load-more bv-focusable" type="button"> is not clickable at point (674,670) because another element <div class="gdpr-cookie-layer gdpr-cookie-layer--show"> obscures it
I've tried getting the element and then clicking it:
element = driver.find_element_by_class_name('bv-content-btn-pages-load-more')
driver.execute_script("arguments[0].click();", element)
I also tried doing it using an action chain:
element = driver.find_element_by_class_name('bv-content-btn-pages-load-more')
ActionChains(driver).move_to_element(element).click().perform()
What is the actual issue here and how do I fix it?

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '[class="gdpr-cookie-layer__btn gdpr-cookie-layer__btn--submit gdpr-cookie-layer__btn--submit--all"]'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".bv-content-btn-pages-load-more"))).click()
you have to first close the accept cookie dialogue , else selenium cannot click the load more element as the accept_cookie screen is in forground

Related

Element is found but not clickable

I'm trying to find an element by it's id, click on it and download a file.
driver.get(url);
driver.implicitly_wait(60);
time.sleep(3)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
href = element.get_attribute('href')
value = href.split('/')[-1]
print(value);
element.click(); # Error
Error
element click intercepted: Element is not clickable at point (110, 1003)
I've tried Xpath, and CSS path too. All give the same error. If I check for the visibility then it times out. But I can manually see that the element is visible
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'text of the link')]")))
At last, I tried this code.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
ActionChains(driver).move_to_element(element).click().perform()
But it gives error
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
While identified the element, page is still loading, may be the reason it is clicking on somewhere else.
Scroll down to the element and then wait for 1 to 2 seconds and then click.
It is working perfectly for me.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
element.location_once_scrolled_into_view #scroll down to element
time.sleep(2) # wait for 2 seconds , 1 sec working as well
element.click()

Scroll to a button with Selenium Python

I'm working on a webscraping script for a school project. I have to scroll down to a button to click, but I can't make it work.
I tried the following code:
driver.get('https://www.goodreads.com/book/show/28187.The_Lightning_Thief');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(5)
element = driver.find_element(By.CLASS_NAME,"Button Button--transparent Button--small")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
I recieved the following error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".Button Button--transparent Button--small"}
I also tried this, but it didn't work either:
driver.execute_script("arguments[0].scrollIntoView();", element)
When you scroll down on this page it will load in more reviews, so I guess that the button in the bottom I want to click don't load in, so I also tried to scroll down a few times and then try to scroll to the element, but it didn't work.
Can someone please help out?
EDITED:
I also tried this code:
driver = webdriver.Chrome()
driver.get('https://www.goodreads.com/book/show/13335037-divergent');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(3)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = driver.find_element(By.CSS_SELECTOR, "div.Divider.Divider--contents.Divider--largeMargin")
driver.execute_script("arguments[0].scrollIntoView();", element)
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
This will scroll down to the bottom once end let the remaining reviews load in, then get the element and scroll again. But it's not scrolling down enough so the See all reviews and ratings won't be in view and can't be clicked.
Your selector is invalid since you're trying to pass multiple class names to search by class. You need to pass single #class only or to use another locator type, e.g.
element = driver.find_element(By.CSS_SELECTOR, ".Button.Button--transparent.Button--small")
P.S. Note that there are 4 elements with the same set of class names ("Button Button--transparent Button--small"), so search by class names is not a good option in this case
element = driver.find_element(By.XPATH, '//a[.="See all reviews and ratings"]')

clicking an 'input' in python selenium

<input class="jss1805" name="confirmWeight" tabindex="0" type="checkbox" data-indeterminate="false" value="">
The code^ is from a html website and its a checkbox which I want to click. Except it's an input rather than a button or td. Am I able to click this as when I run selenium it seems to not find it.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[2]/main/form/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[1]/div/div/label/span[1]/span/input"))).click()
This finds the element but doesn't click it.
Try the following:
from selenium.webdriver.support.wait import WebDriverWait
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name=confirmWeight]")))
element = driver.find_element_by_css_selector("input[name=confirmWeight]")
element.click()
Explanation:
First you need to wait till your element is clickable. For this you have to define the locator which is unique.
Find the element and click it
If you want to make an input use:
element.send_keys("`your input")
If you want to click on svg, that is a child use: input[name=confirmWeight]>svg
I don't see it in your html. It looks like the info you provided is not enough.

selenium wait until all my list element is clickable

here is my code:
this all_plus_icons returns a list of 10 elements of the page which I need to wait until all these elements are clickable in selenium and click each of them.
all_plus_icons = driver.find_elements_by_xpath("//*[#id='block-views-tenders-block']/div/div/div[2]/div//div[1]/h5/span/i")
for i in all_plus_icons:
time.sleep(10)
i.click()
the above code sometimes generates an error as :
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <i class="ic ac-icon-add"></i> is not clickable at point (831, 605). Other element would receive the click: <div class="cc_banner cc_container cc_container--open">...</div>
(Session info: chrome=87.0.4280.88)
I used this :
WebDriverWait(driver , 20).until(EC.element_to_be_clickable((By.XPATH , "//*[#id='block-views-tenders-block']/div/div/div[2]/div//div[1]/h5/span/i")))
but it does not work. how can I make sure all the elements are clickable.
this is the website.enter link description here
It seem that you need to accept Cookies to perform your actions. Try to do this first before clicking elements:
driver.find_element_by_link_text('Got it!').click()
WebDriverWait(driver , 20).until_not(EC.presence_of_element_located((By.CLASS_NAME , "cc_banner")))
Try run this code
for i in all_plus_icons:
time.sleep(10)
i.click()
driver.refresh

Can't click the button to view more results, getting exception element not clickable

I am trying to scrape some data from this site https://easy.co.il/list/Shopping, when you scroll down the list of businesses it has a button at the end to view more results, when I try to click it using .click function it raises an exception that element is not intractable, I have also tried using Keys.ENTER still same exception, I have tried waiting for the element to be clickable using this code.
results = driver.find_elements_by_xpath('//div[#class="biz-item "]')
print(len(results))
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
the len(results) prints 25 which is all the businesses visible till the view more results button.
I have also tried locating the button, the element is visible on the page but just not clickable.
Can someone please look in to this? Thankyou!
You need to scroll the element before click.Use location_once_scrolled_into_view to scroll the element.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()
If you still gets the error then induce java scripts executor to click.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", button)
Use .visibility_of_element_located method, then scroll:
button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()

Categories