I am unable to close a popup box with Selenium. Below is the code I have written, it returns an exception. Please see the code below.
driver = webdriver.Chrome()
driver.get("https://www.google.com/webhp#q=home+depot+san+francisco&lrd=0x808f7c5c63124c7b:0x32c19e9988b2aa90,1,")
driver.find_element_by_xpath('//div[#class="_wzh"]').click()
# selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Thanks,
It take some time to open your popup. So you need to wait for few seconds until the popup opens and close button get visible.
User Explicitwait condition until visibility of element like below :
element = WebDriverWait(driver, 60).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div>._wzh"))
)
element.click()
Instead of trying to click on element, you should use the built-in support.
Following should work.
alert = driver.switch_to_alert()
alert.accept
Docs
this is because the element is present on an <iframe> tag; so first you need to switch to iframe and then interact with the element:
driver.switch_to.frame driver.find_element_by_css(' #gsr > iframe')
then click on the element:
driver.find_element_by_xpath('//div[#class="_wzh"]').click()
Related
I am trying to click on the "Next page" in Python-Selenium. The element and its path are seen, the buttom is being clicked but after clicking an error is shown:
"StaleElementReferenceException:stale element reference: element is not attached to the page document"
My code so far:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located(\
(By.XPATH, butn)))
print (element.is_enabled())
while True and element.is_enabled()==True:
driver.find_element_by_xpath(butn).click()
The error is one element.is_enabled()==True after clicking
Can someone help?
When you search elements in Selenium then it doesn't keep full objects but only references to objects in DOM in browser. And when you click then browser creates new DOM and old references are incorrect and you have to find elements again.
Something like this
# find first time
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, butn)))
print(element.is_enabled())
while element.is_enabled():
driver.find_element_by_xpath(butn).click()
# find again after changes in DOM
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, butn)))
print(element.is_enabled())
again. This is the second time I am asking this question and I really want to figure out what happens.
Summary: I want to click a "agree and continue" button on paypal with selenium. The button changes from "continue" to "agree and continue". I tried various ways of clicking it and sometimes it works, sometimes it doesn't. It prints "clicked" as it's already clicked, but it won't proceed to the next page.
"continue" button (before the change)
<button class="ppvx_btn___5-8-2" aria-live="assertive" id="payment-submit-btn" data-testid="submit-button-initial" data-disabled="true" xpath="1">Continue<span class="ppvx_btn--state__screenreader___5-8-2"></span></button>
"agree and continue" button (after the change)
<button class="ppvx_btn___5-8-2" aria-live="assertive" id="payment-submit-btn" data-testid="submit-button-initial" data-disabled="false" xpath="1">Agree & Continue<span class="ppvx_btn--state__screenreader___5-8-2"></span></button>
What I've tried:
mainagree = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//button[#id='payment-submit-btn'][#data-
disabled='false']")).click()
print('clicked')
I also tried to_be_clickable
mainagree = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//button[#id='payment-submit-btn'][#data-
disabled='false']")).click()
It works like half of the time which confuses me a lot, and sometimes I got this error when it didn't work
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (951, 965)
(Session info: chrome=91.0.4472.27)
First of all try locating the element by this xpath:
//button[#data-testid='submit-button-initial']
This locator should not change by clicking the element.
Second: Try using visibility_of_element_located expected condition instead of element_to_be_clickable and if this still not helped add time.sleep(1) delay after the expected condition of visibility_of_element_located or element_to_be_clickable
If you don't want to add a hardcoded delay try scrolling the element in to the view by something like this
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("the_element_id")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
and only after that click on it.
As you can see here sometimes there is no way to overcome this problem without a short hardcoded delay. It can be a short delay, something about 200-300 mill seconds, but this is the only solution if other approaches do not help.
The hardcoded delay should be used in addition and after the expected condition was fulfilled.
The solution seems relatively easy to me. Just wait for exact text:
button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,
"//button[contains(text(), 'Agree & Continue')]"
button.click()
Or,
//button[contains(text(), 'Agree & Continue')]
I have this code:
driver.switch_to.window(window_after)
try:
myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.NAME, '_eventId_confirmed')))
print ("Page 2 is ready!")
except TimeoutException:
print ("Loading took too much time!")
btn = driver.find_element_by_name('_eventId_confirmed')
btn.click()
as you can see I first switch window and then check for an element, get that element (a button) and finally try to click on said button. This works maybe 2 out of 3 times but ever so often does it fail with this error message
selenium.common.exceptions.ElementNotInteractableException: Message: Element <button class="btn" name="_eventId_confirmed"> could not be scrolled into view
When visually looking at the flow when it is executing everything seems fine (my first guess was that the window switch didn't work as expected) and the browser ends up in the expected state where I am able to manually click this button. Interestingly enough, there is no timeout or similar when this error occurs, it happens instantly during execution.
Any ideas what's going on here?
This problem usually arises when the element you are trying to click is present on the page but it is not fully visible and the point where selenium tries to click is not visible.
In this case, you can use javascript to click on the element, which actually operates directly on the html structure of the page.
You can use it like:
element = driver.find_element_by_name("_eventId_confirmed")
driver.execute_script("arguments[0].click();", element)
As your final step is to invoke click() on the desired element, so instead of using the expected_conditions as presence_of_element_located() you need to use element_to_be_clickable() as follows:
try:
myElem = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.NAME, '_eventId_confirmed')))
Here are the 2 options.
Using selenium location_once_scrolled_into_view method:
btn.location_once_scrolled_into_view
Using Javascript:
driver.execute_script("arguments[0].scrollIntoView();",btn)
Sample Code:
url = "https://stackoverflow.com/questions/55228646/python-selenium-cant-sometimes-scroll-element-into-view/55228932? noredirect=1#comment97192621_55228932"
driver.get(url)
element = driver.find_element_by_xpath("//a[.='Contact Us']")
element.location_once_scrolled_into_view
time.sleep(1)
driver.find_element_by_xpath("//p[.='active']").location_once_scrolled_into_view
driver.execute_script("arguments[0].scrollIntoView();",element)
I try to open a browser and click on a button by using a python script
My code:
from selenium import webdriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver')
browser.get('xxx')
browser.implicitly_wait(5)
button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
button.click()
The website opens. It waits 5 seconds and then I see the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#softGateBox > div.button_softgate > a"}
What can be wrong? I use chrome to inspect the button and I perform a right click and click copy selector.
There is few problem with this page
element is inside <iframe> so you have to find <iframe> and switch_to_frame() before you can search element
<iframe> is in external <iframe> so first you have to find external <iframe> and switch_to_frame() before you start to searching internal <iframe>
on small monitor element is invisible so Selenium can click it. You have to scroll page to element and then you can click it.
.
from selenium import webdriver
browser = webdriver.Chrome() #'/usr/local/bin/chromedriver')
browser.get('https://www.facebook.com/SparColruytGroup/app/300001396778554?app_data=DD722A43-C774-FC01-8823-8016BFF8F0D0')
browser.implicitly_wait(5)
iframe = browser.find_element_by_css_selector('#pagelet_app_runner iframe')
browser.switch_to_frame(iframe)
iframe = browser.find_element_by_css_selector('#qualifio_insert_place iframe')
browser.switch_to_frame(iframe)
button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
browser.execute_script("arguments[0].scrollIntoView(true);", button)
button.click()
BTW:
There can be other <iframe> on page so you can't direclty do selector('iframe').
Internal frame have id but it changes every time you load page so you can't do selector('iframe#some_uniqe_id')
I have a code that tell Selenium to wait until an element is clickable but for some reason, Selenium doesnt wait but instead, click that element and raise a Not clickable at point (x, y) immediately. Any idea how to fix this ?
x = '//*[#id="arrow-r"]/i'
driver = webdriver.Chrome(path)
driver.get('https://www.inc.com/inc5000/list/2017')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, x)))
driver.find_element_by_xpath(x).click()
EC.element_to_be_clickable check if element is visible and enabled. In terms of visibility it doesn't cover scenario when element is behind other. Maybe your page use something like blockUI widget and click() occurs before the cover disappears. You can check if element is truly clickable by enriching EC.element_to_be_clickable((By.XPATH, x)) check with assertion that ensure element is not behind by other. In my projects I use implementation as below:
static bool IsElementClickable(this RemoteWebDriver driver, IWebElement element)
{
return (bool)driver.ExecuteScript(#"
(function(element){
var rec = element.getBoundingClientRect();
var elementAtPosition = document.elementFromPoint(rec.left+rec.width/2, rec.top+rec.height/2);
return element == elementAtPosition || element.contains(elementAtPosition);
})(arguments[0]);
", element);
}
This code is in C# but I'm sure you can easily translate into your programming language of choice.
UPDATE:
I wrote a blog post about problems related to clicking with selenium framework https://cezarypiatek.github.io/post/why-click-with-selenium-so-hard/
Here is a link to the 'waiting' section for the Python Selenium docs: Click here
Wait will be like:
element = WebDriverWait(driver, 10).until(
EC.visibility_of((By.XPATH, "Your xpath"))
)
element.click();