Need a help regarding a selenium based pop up.
After I authenticate to a URL, I get a pop up with a checkbox and Open/Cancel buttons. Am trying to click a checkbox on a selenium pop up (looks like a javascript or so ?), and then an Open button.
I tried browser.switch_to.alert() and a few other things, but failed to handle.
browser.switch_to.alert()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\saleh\PycharmProjects\LearnPythonSaleh\venv\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 55, in alert
alert.text
File "C:\Users\saleh\PycharmProjects\LearnPythonSaleh\venv\lib\site-packages\selenium\webdriver\common\alert.py", line 67, in text
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
File "C:\Users\saleh\PycharmProjects\LearnPythonSaleh\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\saleh\PycharmProjects\LearnPythonSaleh\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=94.0.4606.81)
Currently, I handle it using pyautogui as below (but this will need browser to be in focus. looking for a reliable way to handle this)
time.sleep(5)
pyautogui.press("tab") # Press the tab key once to go to the check
box in the browser pop up
pyautogui.press('enter') # Press the ENTER key to tick he check box
in the browser pop up
pyautogui.press("tab") # Press the tab key once to go to the 'Open
button
pyautogui.press('enter') # Press the ENTER key to Click on the 'Open
button
Appreciate any help on this... thanks
Related
This is my current code, but whenever I run it I get an error on the last line
stale element reference: element is not attached to the page document
from selenium import webdriver
url = "https://otctransparency.finra.org/otctransparency/OtcDownload"
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(5)
agree = driver.find_elements_by_xpath("//button[#class='btn btn-warning']")[0]
agree.click()
nonats = driver.find_element_by_link_text('OTC (Non-ATS) Download')
nonats.click()
driver.find_element_by_xpath("//img[#src='./assets/icon_download.png']").click()
driver.switch_to.window(driver.window_handles[0])
driver.find_element_by_xpath("(//div[#class='checkbox-inline'])[2]").click()
driver.find_element_by_xpath("(//div[#class='checkbox-inline'])[1]").click()
driver.implicitly_wait(5)
button = driver.find_element_by_xpath("//img[#src='./assets/icon_download.png']")
print(button.is_displayed())
button.click()
When I run my code in debugging mode line by line, everything works fine without any errors. Any help would be great.
Edit: This is my stack trace
Traceback (most recent call last):
File "C:\Users\derpe\Desktop\python projects personal\testing finra\untitled1.py", line 31, in <module>
button.click()
File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\derpe\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=86.0.4240.75)
The checkbox clicks are triggering a page refresh while you're searching for the download link.
Call sleep to allow the refresh to finish.
driver.implicitly_wait(5)
import time # add this
time.sleep(1) # add this
button = driver.find_element_by_xpath("//img[#src='./assets/icon_download.png']")
print(button.is_displayed())
button.click()
I tried other selenium waits, but they did not work for me. Probably because the element search succeeds before the refresh starts but still clicks to late.
I've been trying to select the Play button on Spotify's webplayer through selenium for a series of tests, but have been unable to successfully target it. I've tried through CSS Selector:
driver.find_element_by_css_selector("#header .button-primary").click()
and xpath:
driver.find_element_by_xpath("//button[#class=\"button-primary\"]")
This is my error call stack:
Traceback (most recent call last): File "Spotify Dumbass.py", line
37, in test_spotify_dumbass
driver.find_element_by_css_selector("#header .button-primary").click() File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 396, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 684, in find_element
{'using': by, 'value': value})['value'] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 195, in execute
self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 170, in check_response
raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: Unable to locate element:
{"method":"css selector","selector":"#header .button-primary"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/fxdriver#googlecode.com/components/driver-component.js:10299)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/fxdriver#googlecode.com/components/driver-component.js:603)
The "play" button is inside an iframe, switch to it before locating the element:
driver.switch_to.frame("app-player")
play = driver.find_element_by_id("play-pause")
play.click()
Controlling through Javascript
Is there a JS SDK for the player? Can I send play/pause commands using Javascript?
The Play button doesn’t have any observable events, nor does it listen for any. Playback can only be triggered by the user clicking the button.
We’re certainly looking at creating a JS SDK; it’s a bigger project and right now we’re focusing on quickly releasing APIs to open up the general Spotify ecosystem (catalogue, playlists, etc).
None of these requests are falling on deaf ears, and we’re working hard to bring out as much functionality as we can.
From Sptotify's Dev Page
I have a form to be completed and a button to be pressed on a page which would take me to another URL(post form completion) and that page has another button upon clicking which the results filled in the form will be downloaded in a well arranged pdf format. I am currently having difficulty in navigating to the second URL upon clicking the first URL's button, that is, when I try to select any second window's component, I get the following error.
Traceback (most recent call last):
self.driver.find_element_by_css_selector('.buttons.button-save').click()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 376, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":".buttons.button-save"}
I also tried using explicit wait before I try to click on the second page's button, but it didn't work.
Any help would be appreciated, thanks !
Edit :
When I took a screenshot after clicking the button on the first page and saved the image, I could only see a screenshot of the first URL. Also, I added an explicit wait to locate the presence of an element in the second URL and I got the following traceback
Traceback (most recent call last):
EC.presence_of_element_located((By.ID, "button-save")))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 75, in until
raise TimeoutException(message, screen, stacktrace)
Relevant Code,
#Clicking the save button
self.driver.find_element_by_css_selector('.submit.submit.submit').click()
#Waiting to click on the button in second URL
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "button-save")))
#Saving the screenshot to check in which window it is
self.driver.save_screenshot('file2.png')
#Click the button on the second page
self.driver.find_element_by_css_selector('.buttons.button-save').click()
Here is my Splinter code -
b = Browser()
b.visit("http://boingboing.net")
b.fill("q", "OpenXC")
At this point, I would like to press "Enter" for the search to happen.
This is very similar to the example in the Splinter tutorial page. In this instance, there is really no button element present.
I see that this Search box is a Google Search Box with an id "cse-search-box". I do not know how to trigger search here.
Can you please help?
Metaphy's (thanks, btw) code below generates a traceback for me -
Traceback (most recent call last):
File "/Users/muthu/Desktop/boing.py", line 5, in
b.execute_script('document.getElementsByName("f")[0].submit()')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/init.py", line 58, in execute_script
self.driver.execute_script(script)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 397, in execute_script
{'script': script, 'args':converted_args})['value']
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'waiting for evaluate.js load failed' ; Stacktrace:
at r (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:8360)
at fxdriver.Timer.prototype.runWhenTrue/g (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:392)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:386)
I can not access the http://boingboing.net site, so I take baidu.com for example:
from splinter.browser import Browser
b = Browser('firefox')
b.visit('http://www.baidu.com')
b.fill('wd', 'test')
b.execute_script('document.getElementsByName("f")[0].submit()')
New answer:
browser.fill('some_name', 'some_value\r')
I want to handle a web dialog box under selenium web driver (Internet Explorer) . I am using Python
In my application when I click on Icon, a web dialog box opens which contains some text boxes (Webelements) and I need to click on save button after entering some text. The problem is I dont know whether the focus got switched to the web dialog box or not. Here is my code
driver.find_element_by_xpath("//img[contains(#src,'/images/btn_add.gif')]").click()
driver.switch_to_alert()
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
Here is the error I am getting
Traceback (most recent call last):
File "C:\Users\vthaduri\workspace\LDC\test.py", line 14, in <module>
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 651, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to find element with name == report_cutoff_date'
For your information the webelement is present with same name and cutoff date.
Can some one help me on this.
Try this:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
I think the problem is with the following code -
driver.switch_to_alert();
You want to switch to another dialog box which appears when you perform the first click() operation. I think that this box that appears is not an alert.
You might have to switch to the other dialog box by using
driver.getWindowHandles();
driver.switchTo().window(handle);
You can check an example here.