I'm trying to automate a website using python selenium.
# ----- click on the sign up popup
popup = driver.find_element_by_xpath("/html/body/div[1]/div[4]/section[1]/div/div/div[2]/div/p/a")
popup.click()
click is working fine and the pop-up message is showing perfectly. but after that my I get this error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1263, 589). Other element would receive the click: <div class="mailmunch-inner-overlay" id="mailmunch-inner-overlay-178218df563cebd"></div>
(Session info: chrome=89.0.4389.82)
and the code stop working. what can I do?
please help!
You need to get rid of the overlay. Without the HTML I can only offer a couple of solutions:
Find the dismiss button of the overlay, click it.
Use Javascript to remove the overlay from the DOM
Wait until the overlay disappears
Could it be that you are receiving a described error after you are trying to find an element from that pop-up window? So it's not the line you are thinking that is producing the error. If that is the case, then when you are trying to for example click something in that pop-up window, try the following:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,""))).click()
You need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
popup = driver.find_element_by_xpath("/html/body/div[1]/div[4]/section[1]/div/div/div[2]/div/p/a")
driver.execute_script("arguments[0].click();", popup)
Try invoking click directly to the element so that it bypasses the overlay or click it's close button.
Related
I'm trying to scroll down the popup window here: (note you'll need to click any food item on the page to bring up the popup in question)
https://www.just-eat.co.uk/restaurants-mcdonalds-claphamjunction/menu
I've tried to scroll down this popup using the below code, first identifying the popup scrollbar using its css selector and then sending arrow down keys to it.
popup = browser.find_element_by_css_selector("body > div:nth-child(3) > div:nth-child(4) > div.c-megaModal.MegaModal_c-megaModal_3w2Fg.u-overlay.c-itemSelector.c-modal--popUp--belowMid.is-fullHeight")
popup.send_keys(Keys.ARROW_DOWN)
However, I get the following error message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I managed to scroll down fine using FireFox but I can't get it to work on Chrome (need to use Chrome for other reasons).
Any help would be appreciated
EDIT:
I tried the following but it's not scrolling (no error message appears it just passes through the code without scrolling).
wait = WebDriverWait(browser, 20)
for _ in range(8):
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//div[contains(#class,'MegaModal_c-megaModal-document--scrollable')]")))
browser.execute_script(
"document.querySelector('div[class*=MegaModal_c-megaModal-document--scrollable]').scrollDown += 250")
You are using a wrong locator
This element is not directly interactable by user. This is why you can't send keys to it with Selenium.
Instead of sending arrow keys to that element you can scroll it with JavaScript executor.
Please try this
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class,'MegaModal_c-megaModal-document--scrollable')]")))
driver.execute_script("document.querySelector('div[class*=MegaModal_c-megaModal-document--scrollable]').scrollDown += 250")
In order to use explicit conditions as above you will need these imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
And to initialize wait object
wait = WebDriverWait(driver, 20)
I'm using Selenium Webdriver to check a box in a form and click save on the same page. The checkbox works fine, but cannot click save. I have tried multiple solutions without a working solution. I add a sleep as after clicking save manually it does take a while to process.
Here is the html for the save button:
<input class="cbi-button cbi-button-apply" type="submit" name="cbi.apply" value="Save">
Here is my code using xpath (attempt 1):
driver.find_element_by_xpath("//input[#type='submit']").click()
time.sleep(20)
Output from attempt 1:
ElementNotInteractableException: Message: element not interactable
Attempt 2 using action chains as suggested by another answer:
button = driver.find_element_by_class_name(u"cbi-page-actions")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
time.sleep(20)
There are no errors raised in this second attempt but from watching the browser the save button did not seem to be clicked. I have also checked afterwards on the page and the changes I added were definitely not saved.
I've also attempted using webforms however I have the opposite problem where I am able to save the form but cannot select the checkbox.
Try this
element = driver.find_element_by_xpath("//input[#type='submit']")
driver.execute_script("arguments[0].click();", element)
You should wait before, may be the element was not loaded properly, not after triggering the click
Solution 1: Direct click with worst type of Explicit wait
time.sleep(20)
driver.find_element_by_xpath("//input[#type='submit']").click()
Solution 2: Using Actions chain :
button = driver.find_element_by_css_selector("input[value='Save']")
ActionChains(driver).move_to_element(button).click(button).perform()
Solution 3: Using dynamic Explicit wait:-
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, "cbi.apply"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Solution 4: Using Js
button = driver.find_element_by_name("cbi.apply")
driver.execute_script("arguments[0].click();", button)
I just started working with Selenium webdriver to try automate clicking a button in a discord chat based on preconditions. The problem I am having is being able to classify this button.
For starters, the HTML code is as follows:
<div tabindex="0" class="reactionInner-15NvIl" aria-label="♥️, press to react" aria-pressed="false" role="button"><img src="/assets/1afd3c799f3e158662a70498e83e2a87.svg" alt="♥️" draggable="false" class="emoji"><div class="reactionCount-2mvXRV" style="min-width: 9px;">1</div></div>
What I first tried to do was to find_element_by_xpath:
driver.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[48]/div[1]/div[2]/div[2]/div[1]/div/div').click()
But, when a new opportunity for this reaction comes up, the xpath changes:
/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[50]/div[1]/div[2]/div[2]/div[1]/div/div
Notice the only part changing it the div[48] to div[50]
The next thing I tried was to find_element_by_class_name:
element = driver.find_element_by_class_name('reactionInner-15NvIl')
driver.execute_script("arguments[0].click();", element)
The reason I did this was because I was having a problem with simply doing:
driver.find_element_by_class_name('reactionInner-15NvIl').click()
That code would give me an error saying Message: element click intercepted: Element <div tabindex="0" class="reactionInner-15NvIl" aria-label="💗, press to react" aria-pressed="false" role="button">...</div> is not clickable at point (405, 94). Other element would receive the click: <span id="NewMessagesBarJumpToNewMessages_122" class="span-3ncFWM">...</span> and the code wouldn't run. The program runs with the execute_script but it just isn't doing anything, the button is not being clicked. So if anyone has any idea how to be able to click this button, any help would be appreciated!
You can use xpath in a better way than navigate between the divs, like you did the first time.
First of all, if you can change the HTML to use id, or name, thats better.
In this case, you could use this xpath:
//div[#class = "reactionInner-15NvIl"]
When searching by class, this could return multiple results, so use the function find_elements_by_xpath, and then choose the exactly one
Induce WebDriverWait and wait for element_to_be_clickable() and following css selector.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Update:
Induce Javascript Executor to click.
driver.execute_script("arguments[0].click();", WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))))
I have a problem navigating a website using selenium. This is my code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://webtrader.binary.com/v2.2.8/main.html#')
resources = driver.find_element_by_id('ui-id-1')
resources.click()
However, I get the exception:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <ul id="ui-id-1" class="ui-menu ui-widget ui-widget-content ui-menu-icons"> could not be scrolled into view
I don't understand where I went wrong. I am trying to access 'Historical data' from the dropdown menu labeled "Resources". Could someone please help me access it. Maybe I got the id for Resources wrong. You could also check that out.
The element you want to click to open the dropdown is the previous sibling of the element resources
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import By
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(driver, 10)
driver.find_element_by_class_name('resources').click()
historical_data = wait.until(ec.visibility_of_element_located((By.ID, 'ui-id-4')))
There are many things happening here. First of all, your code is lacking a wait. Without it will always fail, as the page is dynamically loaded. Read about Waits.
Secondly, here resources = driver.find_element_by_id('ui-id-1') you are finding the element from the dropdown menu, and then you are trying to click it. But the dropdown menu is not opened. You should click on it, then wait for the option to appear, only then click on the 'Historical data'.
I have a problem trying to automate my browser with Selenium on Python. It's been several hours that I block, and since I'm a beginner .. :(
I explain my problem:
I have to reach click on a box of Recaptcha. To do this, my bot must click on a button on the site, which then displays the recaptcha that I have to validate.
Here are the source page screenshot:
The popup of the recaptcha, in which the checkbox is located
The location of the checkbox that I have to click
I try this code:
time.sleep(5)
browser.switch_to_frame(browser.find_element_by_tag_name("CaptchaPopup"))
browser.switch_to_frame(browser.find_element_by_tag_name("iframe"))
CheckBox = WebDriverWait(browser, 10).until(
browser.find_element_by_id('recaptcha-anchor').click())
time.sleep(0.7)
CheckBox.click()
But the latter returns me an error :(
selenium.common.exceptions.NoSuchFrameException: Message: no such frame
I use Python 2.7.
Do you have a solution ?
Thank you very much in advance!
Try to use below code to handle required check-box:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it(browser.find_element_by_xpath('//iframe[contains(#src, "google.com/recaptcha")]')))
wait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'recaptcha-anchor'))).click()