Running a selenium based exe to scrape data - python

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.

Related

Python: ChromeDriverManager chrome downloads from incorrect url of latest chromedriver

My log shows that the Chromedriver downloads from a 3 part version number rather than a 4-part
10:32:22 INFO About to download new driver from https://chromedriver.storage.googleapis.com/104.0.5112/chromedriver_win32.zip
It should be
https://chromedriver.storage.googleapis.com/104.0.5112.79/chromedriver_win32.zip
(i.e have the 4th part of version)
Ive tried setting the latest_release_url to a 4 part version number but it doesnt overwrite it. Code below
full_version = '104.0.5112.79'
caps = {"pageLoadStrategy": "normal"}
latest_release_url = 'https://chromedriver.storage.googleapis.com/{LATEST_RELEASE}_{full_version}'
driver = webdriver.Chrome(
ChromeDriverManager(
latest_release_url=latest_release_url).install(),
options=options,
desired_capabilities=caps)
Not sure in which os variant/flavor or specific circumstances or Python version you are seeing the shortened download url as:
https://chromedriver.storage.googleapis.com/104.0.5112/chromedriver_win32.zip
However, on my windows-10 64-bit operating system, x64-based processor system using using webdrivermanager-python v3.5.2, python v3.9.0, selenium4 v4.4.0 and the following line of code:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
I can properly see the full path i.e.
https://chromedriver.storage.googleapis.com/104.0.5112.79/chromedriver_win32.zip
within the logs:
====== WebDriver manager ======
Current google-chrome version is 104.0.5112
Get LATEST chromedriver version for 104.0.5112 google-chrome
There is no [win32] chromedriver for browser in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/104.0.5112.79/chromedriver_win32.zip
Driver has been saved in cache [C:\Users\username\.wdm\drivers\chromedriver\win32\104.0.5112.79]
Update
Though as per selenium/py/CHANGES page Selenium 4.4.2 is the latest release but may be not be stable.
To be on the safer side ideally we need to use the version mentioned in the Downloads webpage, which is v4.4.0

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())

Way to download and install Chrome by script? Python, for Mac

I have a task, and a part of it is to download and install latest Chrome (like step to test something).
And I can't get how I can do it by script (Python). I can download it, but after it I have to execute a lot of actions, like path to install, clicking 'next' in installing window etc.
Maybe, someone knows how can I do it?
Sorry, for stupid question, I am new in programming.
Script for download Chrome:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.google.com/chrome/?brand=CHBD&gclid=EAIaIQobChMI8ob1rLv26AIVw7LVCh1vgQ5XEAAYASAAEgLHsfD_BwE&gclsrc=aw.ds")
time.sleep(5)
element = driver.find_element_by_id("js-download-hero")
element.click()
time.sleep(20)

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

Selenium test with Python in Internet Explorer

I have written a script that opens a web browser using python and Selenium. It works fine with Firefox using the following code:
from selenium import webdriver
driver = webdriver.Firefox()
When I replace Firefox with IE (the suggested value when I start typing), I get the message IEDriver executable needs to be available in the path.
from selenium import webdriver
driver = webdriver.IE()
Download IE Drivers based on your OS (Windows 32 or 64 bit)
a. Download Windows 32 bits driver
OR
b. Download Windows 64 bits driver
Extract the zip and copy IEDriverServer.exe file to some location e.g. E:\IEDriver
Write the following script
from selenium import webdriver
browser = webdriver.Ie("e:\\IEDriver\\IEDriverServer.exe")
Run the script, it should open IE browser...
Selenium with Python bindings in IE:
There are 2 ways to run Selenium python tests in Internet Explorer. I'm considering Windows (Windows 10 in my case):
Prerequisite: Download IE Driver based on your OS from the site: http://docs.seleniumhq.org/download/
32 bit Windows IE
64 bit Windows IE
Way 1:
i) Extract the downloaded zip file in a directory/location of your choice
ii) Set the executable path in your code as below:
self.driver = webdriver.Ie(executable_path='D:\Selenium_RiponAlWasim\Drivers\IEDriverServer_x64_2.42.0\IEDriverServer.exe')
OR,
self.driver = webdriver.Ie("D:\\Selenium_RiponAlWasim\\Drivers\IEDriverServer_x64_2.42.0\\IEDriverServer.exe")
Way 2:
i) Simply paste the IEDriverServer.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:
self.driver = webdriver.Ie()
It means exactly that. Selenium needs the executable to work with IE.
A quick google search led me to this. You need to download the executable and place it somewhere visible. Also, taking a look at this should help clear some things about PATH variables.
In selenium 4 you would need to use service method otherwise you would get depreciated error: You no longer need to download IE driver.
Here are compatible codes for Selenium 4.x IE:
# Internet Explorer Browser version
from selenium import webdriver
from selenium.webdriver.ie.service import Service
from webdriver_manager.microsoft import IEDriverManager
driver = webdriver.Ie(service=Service(executable_path=IEDriverManager().install()))
driver.get('https://www.google.com')

Categories

Resources