Selenium: Unable to download file - python

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

Related

Trying to open a webpage using selenium but getting error

I wrote this simple code:
from selenium import webdriver
webdriver.Chrome().get('www.google.co.in')
but I'm getting this error:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\WebAutomation\venv\Automation.py", line 2, in <module>
webdriver.Chrome().get('www.google.co.in')
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
self.service.start()
File "C:\Users\user\PycharmProjects\WebAutomation\venv\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
Someone please help me. I'm a beginner, please explain/guide in detail (I don't know complex concepts)
You will need additional the Selenium-Chrome-Driver.exe!
The last line of errors tells this to you.
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
The driver itself will be started from your programm and will hold the binding between your program and the browser chrome itself.
The driver.exe can be easily placed in the programm-startup-path (I recommend this for the first steps) or placed where ever you want, but than you must define the special variable of the PATH to the driver.exe.
You can get the driver.exe behind this link:
Main page:
https://chromedriver.chromium.org/getting-started
Download Page:
https://chromedriver.chromium.org/downloads
Please ensure, that your Browser-Version match to the driver-Version.

Click on DIV with selenium [duplicate]

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.

selenium 'nonetype' object has no attribute 'send_keys'

title='this is the title'
I want to locate with python/selenium, within a web page, this line:
<input id="subject" name="subject" maxlength="50" value="" class="nude error" type="text">
I use this code with python-selenium (under Debian):
title = driver.find_element_by_id("subject").clear()
title.send_keys(title)
I got the following error:
Traceback (most recent call last):
File "./basic0", line 49, in <module>
titre.send_keys(title)
AttributeError: 'NoneType' object has no attribute 'send_keys'
note:when the script stops because of this error, the mouse cursor is at the right at place within the web page; but I cannot find a way to send_keys to fill in the input
I also tried:
title = driver.find_element_by_xpath("div[contains(text(),'subject')]")
title = driver.find_element_by_xpath("//form[input/#id='subject']")
title = driver.find_element_by_xpath("//form[input[#name='subject']")
but it does not work; furthermore the mouse cursor is not at the right place.
then I tried a later selenium version:
I completly purge python-selenium package under Debian (which is selenium v. 2.53)
then
pip install selenium==3.3.1
This time, when I launch the script it says that geckodriver is missing:
so,
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux32.tar.gz
tar -xvzf geckodriver-v0.23.0-linux32.tar.gz
chmod 755 geckodriver (I also tried 777)
mv geckodriver /usr/local/bin/ (so it's in my PATH)
now when I launch the script, here is the error message I got:
Traceback (most recent call last):
File "./basic0", line 13, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refuse
firefox window pops-up, and then shutdown when the script stops
You've assigned method call (clear()) which returns None to title variable while you need to define WebElement and call methods subsequently as
title = driver.find_element_by_id("subject")
title.clear()
title.send_keys("title")
if you are able to click in the text box but you are unable to type in there because send_keys is not working and you are in hurry or you want to just click without selecting any element then:
please install this module by typing pip install pyautogui
import pyautogui
#use this line to type something
pyautogui.typewrite('Anything you wanna type')
#use this line to press any key
pyautogui.press("esc")
[here][1] is the list of keys you can press
Note : If you are going to minimize the windows pyautogui will start typing at the place you currently clicking or when the pyautogui.typewite() is executing and you are on other pages it will start typing on the page you are in rather then typing on webpage.

How to find elements of a modal pop up window with Selenium Webdriver

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.

CLOSED - Python Selenium Can't find elements

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");

Categories