Setting default download directory Selenium ChromeDriver - python

I see this question has been answered several times before but none of the solutions appear to be working anymore.
My script looks like:
from selenium import webdriver
chromeoptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/root/path/to/folder"}
chromeoptions.add_experimental_option("prefs",prefs)
browser = webdriver.Chrome(chrome_options=chromeoptions)
Everything is working as normal...but the download directory reset fails silently.
I have tested the rest of the code (but not the download directory reset) on Windows and it is working correctly, other similar scripts (without the download) are working normally too. The chromeoptions object is also correctly showing the target directory under experimental options...so it is presumably being passed to the browser object.
Something is going wrong, I am not sure what? I am using Python 2.7, latest versions of Chrome/Selenium/ChromeDriver on Ubuntu 14.

Related

Selenium opening Brave, but not going to URL. Using Python

Yes, yes, I know everyone is going to be angry at me for asking this question again. But I have looked at every question (related to Brave, Selenium, Python and Linux) in Stack Overflow, but nothing has worked for me. Here is my problem: I run the code. No errors. Brave opens up and says it is being controlled by “automated test software” (good sign). But then it doesn't go to the URL I want it to go to. I have checked the chromedriver and Chromium version, they match. I also use Ubuntu 20.04 LTS. Last thing, I installed Brave using snap.
Here is my code:
from selenium import webdriver
brave_path = "/snap/bin/brave"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
driver = webdriver.Chrome("/bin/chromedriver_linux64/chromedriver", options=option)
driver.get("https://youtube.com/")

Selenium Opens Browser But Refuses to Execute the Next Line of Code

It looks like after opening the browser, Selenium cannot move on for some reasons I can't figure out. No error was ever displayed.
Here is my simple code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Using Chrome to access web
browser = webdriver.Chrome(executable_path = "C:\Program Files\Google\Chrome\Application\chrome.exe")
print ("done")
# Open YouTube website
browser.get("https://www.youtube.com/")
The browser opens just fine, but the print("done") statement is never executed. (In the terminal the word "done" was never printed. So, it infers that the selenium has never finished executing the command to open the browser even though the browser has opened, and I have waited for several minutes.
Thanks in advance to our wonderful StackOverflow community!
The reason is because you are using chrome.exe which is for browser. Instead you should download chromdriver.exe, Please download from here. You should download Latest stable release: ChromeDriver 94.0.4606.61 (as on 3rd-oct-2021). Keep that in your automation directory and any directory of your preference.
driver_path = r'C:\\Users\\username\\Desktop\\Automation\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path)
Note that, in place of driver_path, you should give the path where you've kept the chromdriver.exe
You should use chromedriver.exe instead of the path to your chrome.exe.
Download the chromedriver suitable for your chrome version, from here:
donwload Chromedriver.exe
Afterwards, do something like this:
browser = webdriver.Chrome("E:\YourPathToChromeDriver\chromedriver.exe")
Try these steps:
Check your Google Chrome version here "chrome://settings/help"
Download chromedriver.exe from "https://chromedriver.chromium.org/downloads"
Change executable path to newly downloaded file.
As mentioned above, this issue is the chrome.exe. You need to use a chromedriver instead. You can download one manually here https://chromedriver.chromium.org/downloads and then set the path to it like you did to the chrome.exe.
However, instead of downloading the chromedriver manually, I recommend using a library on GitHub which does that for you and loads it from cache if there is already one installed. (I'm not the owner nor the maintainer of this repository, but I do find it rather simple to use.)
https://github.com/SergeyPirogov/webdriver_manager
First you'd need to pip install webdriver-manager and then you can use it as following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

Running a selenium based exe to scrape data

What is working
I have created a Python file that opens a Firefox browser using Selenium and scrapes data to a excel file. I have also converted this file to exe. No errors were created during exe. This was done by Pyinstaller.
What is not working.
When I run the new created exe on official laptop it doesn't open a browser or give any output. Just a command window opens and closes immediately.
What is my intention.
My intention is to share this script with colleagues so that the data needed can be easily scraped to excel file on any PC and would be efficient process rather than looking up the same on website. I would not want to them to have any admin access to change anything on their PC. Also, I do not want them to have multiple browsers opening up on their PC.
Question
Do I need to install and provide geckodriver path on every PC? Perhaps this is the reason Firefox is not opening despite being available?
Code Below
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
productName = []
number = []
divList = []
driver = webdriver.Firefox()
driver.get("website")
radioBtn = driver.find_element_by_id("ContentPlaceHolder1_company")
radioBtn.click()
element = driver.find_element_by_id("ContentPlaceHolder1_TextBoxInput1")
element.send_keys("CompanyName")
Search = driver.find_element_by_id("ContentPlaceHolder1_view1Continue")
Search.click()
driver.implicitly_wait(10)
driver.find_element_by_id("ctl00_ContentPlaceHolder1_92564").click()
driver.implicitly_wait(2)
divList = driver.find_elements_by_class_name('nopgbrk')
for div in divList:
number.append(div.find_element_by_xpath('a').text)
productName.append(div.find_element_by_xpath('span').text)
df = pd.DataFrame({'Product':productName, 'Registration': number})
df.to_excel("C:\Python\Scrapeddata\Scrape.xls")
I anticipate, the reason for this would be the incompatibility of chromedriver version and the chrome version of your official laptop.
In the development phase, the system on which it was being developed had chrome, whose version was compatible with selenium chromedriver version. So it ran and processed output successfully. But your official laptop did not have the same version that of your system's chrome, so it is not compatible.
Please check chrome version by going to settings -> About Chrome.
Download chromedriver for the same version which you checked above from here and then create exe if it supports:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Or if you know the version of your chromedriver the easiest would be to uninstall chrome on your official laptop and install chrome of the same version that of chromedriver.

Message: 'chromedriver' executable needs to be in PATH

So, I am using OS X El Capitan with Eclipse (Neo) and Python. I am wrote some Python with Selenium scripts.
These scripts were working fine.
Then I upgraded from OSX Sierra to El Capitan
Note: this is about the only major change
that I did to my setup.
When I tried to run the same scripts, now, I get the
Message: chromedriver executable needs to be in PATH
As far I can tell chromedriver IS on my PATH.
Now, I can only make my scripts work if I hard copy the path to my chrome driver, which is not the most elegant way to do things, as we all know.
Has anyone else has/had this issue? Thoughts on any configuration with my Eclipse + Python.
My Eclipse and Python project screen shot
my PYTHONPATH screen shot
I dont know about python. But the implementation for the webdriver should be same irrespective of the language. Here you have called the chrome webdriver, but have you defined the path for chromedriver.exe in you code? For example, in java it looks like this
System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-3.4.0/chromedriver.exe");
driver = new ChromeDriver();
The error you are getting is referring to the chromedriver.exe not the python environment.
Python Solution from Google documentation:
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
First of all verify your driver path , if you open a command prompt and type in chromedriver and hit Enter. If you get message "Starting ChromeDriver 2.15.322448 .." your path is correctly set up.
In python, alternate solution is you can use
driver = webdriver.Chrome("path_to_driver/chromedriver.exe")
Hope this should work !

Selenium Chromedriver launches Chrome, but doesn't open website (new Chromedriver, same old issue)

I am having trouble using Selenium Chromedriver on Windows 7. To display the problem, I've boiled it down to a simple script to simply launch the New York Times website:
from selenium import webdriver
# --LOCATIONS --
# The Chrome app:
# C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
# The Chrome binary:
# C:\Python27\Scripts\chromedriver.exe
chromedriver_path = "C:\Python27\Scripts\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver_path)
driver.get('https://www.nytimes.com/')
The Chrome Browser launches (leading me to speculate that there's nothing with the Chrome application path), but rather than going to the NYT website, the following happens:
The string data:, appears in the URL address bar, and 2 alert notifications come up: one that says "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." and another that says "Disable developer mode extensions: Extensions running in developer mode can harm your computer. If you're not a developer, you should disable these extensions running in developer mode to stay safe."
This didn't happen when I used Selenium for Firefox- so I'm not sure what to do with Chrome. I've tried looking this issue up on the internet beforehand, but all the issues/solutions are dated from a few years back (2014-2015), and I believe the Selenium packages and Chromedriver binaries have been updated since then.
Does anyone know how I can get my code working? Thank you in advance.
I'd have to see your computer to examine how Chromedriver is installed, but as that's not quite feasible, I would at least recommend uninstalling any chromedriver executables on your computer and then downloading it into your project's directory.
It's really just an IT rule-of-thumb; if you've ruled out every other issue that you're aware of, then there's a good chance the problem is something you're not recognizing. Start at square 1 and reinstall Chromedriver.
You can disable Developer mode extension by following code(java)
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver","F:\\Stuff\\Jars\\chromedriver.exe");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://www.nytimes.com/");

Categories

Resources