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()
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 am new in programming (specifically Python Selenium), I have a question regarding pasting a data into an input box, it seems not working if I will paste data on another website, I will received an error. But if I use just one website (either of the two) in pasting the data it is working fine.
Here is my codes below.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\Pashu\AppData\Local\Programs\Python\Python37\chromedriver.exe')
driver.get('https://tinyurl.com/')
inline_tinysite = driver.find_element_by_xpath('//*[#id="url"]')
inline_tinysite.send_keys("https://www.teamxtools.com/content/images/project-7/stack-324/exif/2dehands-velgen-volkswagen-golf-7.jpg")
submit = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/form/input[3]')
submit.click()
driver.execute_script("window.open('https://cutt.ly/', 'new window')")
inline_cuttly = driver.find_element_by_xpath('//*[#id="link"]')
inline_cuttly.send_keys("https://www.teamxtools.com/content/images/project-7/stack-324/exif/2dehands-velgen-volkswagen-golf-7.jpg")
submit2 = driver.find_element_by_xpath('/html/body/div[1]/section[1]/div/div/div/div/div/button')
submit2.click()
#time.sleep(8)
Here is the error message as text:
C:\Users\Pashu\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Pashu/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/Helloworld.py
Traceback (most recent call last):
File "C:/Users/Pashu/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/Helloworld.py", line 14, in <module>
inline_cuttly = driver.find_element_by_xpath('//*[#id="link"]')
File "C:\Users\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="link"]"}
(Session info: chrome=81.0.4044.138)
Process finished with exit code 1
it was stuck on window 1, that is why element //*[#id="link"] is not found. I just added
driver.switch_to.window(driver.window_handles[1])
for it to search on the second window.
This question is very similar to Selenium webdriver can't find elements at chrome://downloads
I'm trying to use Selenium with Python (3) to get at the cancel button on the chrome://downloads page. My use case is that I have an obfuscated link for which a random token is generated every time a user clicks on it. If you don't click on it, you can't start the download (it seems to fire a piece of js that generates the token, but I haven't been successful in digging through the code to figure out how that happens).
For my test to pass, all I need is to verify that:
The download starts (and doesn't give a 404), and
The file it's trying to download is the right size.
The way that I'm trying to accomplish this is by triggering the download by clicking on the button element, then having Selenium open chrome://downloads, cancel the download, and capture the file size of the file that it attempted to download.
In theory this seems like it should work, the stumbling block is trying to access any elements in the #shadow-root tags on the chrome://downloads page. The solution to the other question which I linked above unfortunately no longer works:
driver = webdriver.Chrome("chromedriver.exe")
driver.get("chrome://downloads/")
manager = driver.find_element_by_css_selector('body/deep/downloads-manager')
item = manager.find_element_by_css_selector('body/deep/downloads-item')
shadow = driver.execute_script('return arguments[0].shadowRoot;', item)
link = shadow.find_element_by_css_selector('div#title-area>a')
file_url = link.get_attribute("href")
... as it fails on the item declaration line:
>>> item = manager.find_element_by_css_selector('body/deep/downloads-item')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 430, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 659, in find_element
{"using": by, "value": value})['value']
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"body/deep/downloads-item"}
(Session info: chrome=80.0.3987.149)
This is out of my area of expertise, and any help in figuring out how to get at the cancel button would be greatly appreciated.
Please try the below.
driver.execute_script("document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector("cr-button[focus-type='cancel']").click()")
If you need more information how to work on the shadow-root elements, please refer here. And if you want to work on the downloads then you can refer to this and update the js as per your requirement.
Hi I'm new to python and have been giving a shot at web scraping.
This is what my code looks like:
print('What is the term to be searched?')
term = input()
browser = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver')
browser.get('https://google.com/search?q=' + term)
try:
link = browser.find_element_by_link_text("Hello")
# (I want to make it so the first page has no links of interest, so I can proceed to clicking the next page)
except NoSuchElementException:
nextPage = browser.find_element_by_css_selector('Css of next page button)
nextPage.click()
So the code above has giving me errors so I tried just taking out the 'link=browser.find_element' and searching the term and proceeding to the next page by Css_selector, but I got Traceback error.
Any help would be appreciated. Thanks
Here is the code message:
Traceback (most recent call last):
File "/Users/shaun/Documents/Python/mwpractice.py", line 17, in <module>
nextPage = browser.find_element_by_css_selector('''<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>''')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 498, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element
'value': value})['value']
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>" is invalid: InvalidSelectorError: '<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>' is not a valid selector: "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>"
You have to read error messages more carefully.
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression ..."
It is a very clear message - the CSS selector provided by you is wrong.
If you want to click next page button, you can use xpath:
browser.find_element_by_xpath('//*[#id="pnnext"]/span[2]').click()
Correct CSS selector for next page button is:
#pnnext > span:nth-child(2)
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.