This question already has an answer here:
"unknown error: cannot read property 'scrollleft' of null" in Chrome using selenium
(1 answer)
Closed 3 years ago.
I'm trying to click on "retweet" with selenium.
I found this way with javascript:
document.querySelector('[data-testid="retweet"]').click()
document.querySelector('[data-testid="retweetConfirm"]').click()
So I implemented in selenium with something like this:
firstJS = "document.querySelector('[data-testid=\"retweet\"]').click()"
secondJS ="document.querySelector('[data-testid=\"retweetConfirm\"]').click()"
time.sleep(5)
driver.execute_script(firstJS)
time.sleep(3)
driver.execute_script(secondJS)
and i get this error:
Traceback (most recent call last):
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 75, in <module>
main()
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 67, in main
retweet(link, driver)
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 43, in retweet
driver.execute_script(firstJS)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of null
(Session info: chrome=1.2.3.4)
EDIT: I added some time.sleep() and the first line works, but not the second
This might be due to page loading try adding wait until page load.
Related
This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 3 years ago.
I have a very simple code where I am looking for a class and when that class doesn't exist I want to catch that and update a status in my CSV. I am implementing this like:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
invalid = browser.find_element_by_class_name('_3lLzD')
try:
Update status
except NoSuchElementException as e:
Update Status
It still throws me the error:
Traceback (most recent call last):
File "C:/Users/Krenovate/Desktop/automate/automate/automate.py", line 35, in <module>
invalid = browser.find_element_by_class_name('_3lLzD')
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Krenovate\PycharmProjects\mxrecord\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: Unable to locate element: ._3lLzD
What am I doing wrong
Per the documentation, the locator function is designed to throw an exception if it is not found. Therefore, the exception that you get is well within the expected behavior.
If you are unsure of the presence of the element, the easiest way is to place it within a try block
try:
invalid = browser.find_element_by_class_name('_3lLzD')
Update status
except NoSuchElementException as e:
Update Status
or use explicit waits with a timeout.
I am using Selenium webdriver with firefox to download real states data from a website. Once the inputs are entered a pop up window will ask to either select 'Cancel' or 'Save File'.
The problem is that I'm not able to find how to 'click' these buttons. I know it is already the current window in my driver because I am able to change the enter image description here position.
If I dodriver.find_element_by_xpath('//*'), this is the error I get:
Traceback (most recent call last):
File "<ipython-input-131-e87e382e3fb9>", line 1, in <module>
driver.find_element_by_xpath('//*')
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 295, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 756, in find_element
'value': value})['value']
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: No such content frame; perhaps the listener was not registered
I've searched on the web for solutions and most of them mention switching to alert, driver.switch_to_alert().accept(). This is the error I'm getting:
Traceback (most recent call last):
File "<ipython-input-142-bd939a2be33d>", line 1, in <module>
driver.switch_to_alert().accept()
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/common/alert.py",
line 81, in accept
self.driver.execute(Command.ACCEPT_ALERT)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: No tab modal was open when attempting to get the dialog text
This issue is very similar to Not able to locate element on a modal pop up window : selenium, but I don't understand how the accepted answer there was able to find the selectors.
Any ideas how to solve this issue would be greatly appreciated.
I have a problem with selenium with python 3.5, after install all the windows updates all my selenium script broken, I receive every time the same error:
Traceback (most recent call last):
File "C:/Users/Carlo/Desktop/CEx/src/IE.py", line 12, in
a=driver.find_element_by_xpath("//*[#id='un']")
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to find element on closed window
But I really don't understand why because the window of IE is open!
This is my code (simple just to check why I can't make it work more):
import time from
selenium import webdriver
driver = webdriver.Ie()
driver.get('http://gala.test-platform.celtrino.com/Login.aspx')
time.sleep(10)
driver.find_element_by_xpath(".//*[#id='un']")
The code fail every time in the last line and I double check with firepath and the xpath is correct so I don't really understand why it's not working.
I knew, IE has problems with xpath. It does not support xpath directly. It needs third party tools to do this. So, I suggest you to try cssSelector or any other options instead. Since, the element has an ID so you could use this. It's better.
driver.find_element_by_id("un");
I'm currently in CH11 from the "Automate the Boring Stuff with Python" book and I'm going over the Selenium module. I'm trying to move to the end of a page but I'm getting some problems. I also tried to look similar problems in this site and tried the solutions suggested without success unfortunately. Here's my code, when I type it into the IDLE Shell:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser= webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem= browser.find_element_by_tag_name('html')
type(htmlElem)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>
htmlElem.send_keys(Keys.END) # Error
Exception -:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
htmlElem.send_keys(Keys.END)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not visible
Apparently the html element is not visible? I don't understand how so since it seems to locates the html element just fine as seen on the code without any problems but the Key.ENTER is where I'm getting the error.
Any help would be appreciated.
Just tested the following with Chrome driver and it works (It should also work with Firefox):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('http://nostarch.com')
body_elem = browser.find_element_by_tag_name('body')
body_elem.send_keys(Keys.END)
I am trying to automate downloading mp3 files from Youtube using this site.
Objective:
Visit download website
Paste Youtube link
Click "Convert Video"
Wait for download link to appear, then click it
When I use browser.find_by_id('dl_link').click(), my code executes without any errors but the file is not downloaded. Why?
When I use browser.click_link_by_partial_text('Download'), I get the error shown below.
Here is my code:
from splinter.browser import Browser
import time
with Browser() as browser:
browser.visit("http://www.youtube-mp3.org")
browser.find_by_id('youtube-url').fill("https://www.youtube.com/watch?v=lgT1AidzRWM")
browser.find_by_id('submit').click()
if browser.is_element_present_by_id('dl_link'):
time.sleep(2)
browser.click_link_by_partial_text('Download')
# browser.find_by_id('dl_link').click()
print "Clicked Download"
time.sleep(2)
Here is the error I am getting:
Traceback (most recent call last):
File "/Users/anon/Dropbox/Programs/Proj/splinter2.py", line 11, in <module>
browser.click_link_by_partial_text('Download')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/__init__.py", line 332, in click_link_by_partial_text
return self.find_link_by_partial_text(partial_text).first.click()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 539, in click
self._element.click()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 233, 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 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver#googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver#googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///var/folders/wj/t56fgsms4rdcgptcy94k2w8m0000gn/T/tmpAVUrd2/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
[Finished in 7.2s with exit code 1]
[shell_cmd: python -u "/Users/anon/Dropbox/Programs/Proj/splinter2.py"]
[dir: /Users/anon/Dropbox/Programs/Proj]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
I have already checked other questions with the same (Error: “Element is not currently visible and so may not be interacted with” with selenuim) title, but I am still not able to resolve this since the Download button is visible in my case.
Any help would be much appreciated
EDIT:
Whenever I try to set a profile using browser = Browser('firefox', profile=profile), I get the following error:
Traceback (most recent call last):
File "/Users/adb/Dropbox/Programs/Proj/Youtube Playlist MP3/splinter2.py", line 11, in <module>
browser = Browser('firefox', profile=profile)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/browser.py", line 63, in Browser
return driver(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/firefox.py", line 23, in __init__
firefox_profile = FirefoxProfile(profile)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 77, in __init__
ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 171, in copytree
names = os.listdir(src)
TypeError: coercing to Unicode: need string or buffer, FirefoxProfile found
When I use browser.find_by_id('dl_link').click(), my code executes without any errors but browser.click_link_by_partial_text('Download') throws?
Because you interact with a "static" element.
When you hit the "convert video" button, you refresh the DOM. But your driver still works on the old DOM. That's why you don't find by using browser.click_link_by_partial_text('Download')
When it wokrs, the file is not downloaded. Why?
Probably because there's a pop up windows asking you if you want to save and execute it. Check this out