Python selenium headless start with extensions and xfvb - python

I'm trying to run a headless Chrome process with an extension1. I'm using WSL.
For testing purposes I can use one of two browsers:
Ubuntu: Google Chrome 86.0.4240.183
Windows: Version 86.0.4240.183 (Official Build) (64-bit)
For production purposes the Ubuntu's Chrome is the only viable option2.
The rest of the stack:
Python 3.6.9
selenium: 3.141.0
In order to overcome Chrome not being able to start headlessly with extensions I use PyVirtualDisplay.
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600), backend='xvfb')
display.start()
options = ChromeOptions()
options.add_extension('/home/plonca/selenium_draft/extensions.crx')
options.add_argument('--no-sandbox')
windows_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver.exe'
ubuntu_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver'
chrome_driver = webdriver.Chrome(executable_path=ubuntu_chrome_driver
, options=options
)
The problem occurs in the last line: if the windows_chrome_driver is set as executable_path everything is fine. If I set ubuntu_chrome_driver instead, I get an error:
WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://bmlddehkgnjdalnfaecgflmmeknlbohi/_generated_background_page.html
from tab crashed
I wonder what the cause of the error might be. How to make chrome_driver work in the headless environment?
1 The extension is needed so that I can authenticate via a pop-up window. The extension has been created according to instructions found here. Other options such as sending username and password in the URL or automating the click with AutoIt doesn't work.
2 Please note that the Chrome seems to be the only option since using Firefox is a matter of an unresolved issue.

Related

How do I fix this error in selenium webdriver?

I am using selenium pyhthon to try and open amazon.com in google chrome, but I keep getting this error message when I run the code.
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
I have tried to search up this error on stackoverflow, but all I can find is Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac, which I cannot understand. I am using mac M1 and I have the chrome version 92.0.4515.107. My code is
from selenium import webdriver
chrome_driver_path = "/Users/asznee/selenium/chromedriver"
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://www.amazon.com")
See your Google chrome is not installed in default location which is
C:\Program Files\Google\Chrome\Application
it is installed :
C:\Users\asznee\Desktop\Downloads\Google Chrome\Application
right ?
so for solving this issue, your chromdriver.exe needs to know where Google chrome is installed :-
You can try the below code :-
options = webdriver.ChromeOptions()
options.binary_location = "/Users/asznee/Desktop/Downloads/Google Chrome/Application/chrome"
chrome_driver_binary = "/Users/asznee/selenium/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, options = options)
there is no need to specify the path of the chrome driver just keep the chrome driver in the project
just copy and paste in the project
and start initializing the web driver
simply start typing with
WebDriver driver = new ChromeDriver();
and enter the URL of the application
and follow the code
WebDriver driver = new ChromeDriver();
WebDriver is having unimplemented methods
here is chrome driver is implementing these unimplemented methods
chrome driver is just the driver which you're telling the selenium to run the test on the chrome browser.
you can download it on the
https://chromedriver.chromium.org/home
if you want to work with the firefox browser you use the gecko driver.

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python

So I have made a program on one computer using selenium and that worked, Now using it in another computer I get this error:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
Now this same issue was mention in:
Selenium python: Can not connect to the Service %s" % self.path
Selenium python: Can not connect to the Service %s" % self.path
Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver
however the solutions mentioned didnt work.
I am using chrome version 79 and have installed chromedriver 79, I tested writing chromedriver in command line and that works which means path is configured right, I have made sure 127.0.0.1 localhost is also in etc/hosts
Below is my code which works on my computer (so i doubt its an issue with the code):
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome(chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
In the last question I also tried this modification:
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome("C:\\chromedriver.exe",chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
which would instead give me this almost identical error message:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\chromedriver.exe
I am unsure where the issue could lie.
I am using windows by the way.
EDIT: Things I tried and didn't work:
1- running everything as both admin and normal
2- re-installing chrome
3- using the beta-chroma 80 and webdriver 80
4- using normal chrome 79 and webdriver 79
5- Having both the script and the driver in the same directory (while using a
correct path)
6- Having an external path and have it setup as needed
7- Using it in the PATH folder.
8- Adding "127.0.0.1 localhost" to etc/hosts
9- Running service test
I have ensured in every test that everything was in it's correct placement, I have ran a reboot before every new test, and they always give me the same error, which also happens to occur if I had an incorrect path as well, but once I ran the code for a service and it gave me a different error as I had my webdriver in C:/ which required admin privilages, however re-running the test again with the correct privilages gave back the same error
Update the issue isn't exclusive to the chrome driver. Even following setup instructions for either the Firefox or edge drivers end up on the same issues. It makes me suspect that the connection is facing some issue. I have tried running the test codes provided by Mozilla for the setup and it didn't work.
Unsure if that does help much or at all.
This error message...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
You need to take care of a couple of things:
Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:
chromedriver_linux64.zip: For Linux OS
chromedriver_mac64.zip: For Mac OSX
chromedriver_win32.zip: For Windows OS
Ensure that /etc/hosts file contains the following entry:
127.0.0.1 localhost
Ensure that ChromeDriver binary have executable permission for the non-root user.
Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=chrome_options) as driver:
So your effective code block will be:
options = Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox') # Bypass OS security model
options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument("--disable-dev-shm-usage") # overcome limited resource problems
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
Mandatory Considerations
Finally, to avoid incompatibility between the version of the binaries you are using ensure that:
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test as non-root user.
References
You can find a couple of reference discussions in:
Python Selenium “Can not connect to the Service %s” % self.path in linux server
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

Using an adblocker extension with the headless chrome driver using python selenium

I am trying to use an adblocker and running my chrome driver headlessly, doing both separately gives me no error but adding both options gives me the following error:
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html
From what I could understand, the adblock extension is looking for the background page and isn't finding it as it is in headless mode. (it works fine without the headless mode)
Testable code (gives the error):
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# adblocker crx file, downloaded from: https://chrome-extension-downloader.com/
chrome_options.add_extension("Adblocker-for-Chrome-NoAds_v3.2.0.1.crx")
chrome_options.add_argument("headless")
driver = webdriver.Chrome(options=chrome_options)
Things I have tried:
·Different adblocker. (can provide a list)
·Making the code wait at various place. (as the error said it failed to wait for
something)
·Going headless but without an adblocker. (was slower than with an adblocker and the window's GUI showing)
·Asking google multiple times the question. (didn't work...)
If anyone knows a solution to run the chrome browser headless using python selenium while having an adblock extension, I would like to know it too, thanks.
Running selenium in headless mode with extensions is currently not possible and as it seems, Google isn't planning on supporting that any time soon.
You can find more information here

Can't open web browser using selenium chromedriver on windows 10

I'm trying to open a web browser using Selenium Chromedriver on Windows 10 with python in jupyter notebooks through an ubuntu command prompt. I've read stack overflow posts and tried to solve based on their answers, but I'm stuck in a loop where I keep receiving the same 3 errors.
Here is what I have installed:
OS - Windows 10, 1709, 64-bit Selenium - 3.8.1 Chromedriver - 2.45
Chrome - Version 71.0.3578.98 Python - 3.5.2
I tried various websites. The goal is to eventually get to a social media login page, but i'm stuck at opening a new blank web browser.
Here is my starting code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.imdb.com/")
WebDriverException: Message: 'chromedriver' executable may have wrong
permissions.
Then I tried the following:
from selenium import webdriver
chromedriver = "C:/Users/xxxx/AppData/Local/lxss/home/xxxx/chromedriver.exe"
browser = webdriver.Chrome(chromedriver)
browser.get('https://www.imdb.com/')
WebDriverException: Message: 'chromedriver.exe' executable needs to be
in PATH.
Here are the steps I have taken:
I added a PATH under environment variables to the folder - (C:\Users\xxxxx\AppData\Local\lxss\home\xxxx),
I tried using \, and /, and even \
Once I added the PATH, I tried the following two codes (and various versions) and received the same error:
A.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx)
driver.get("https://www.imdb.com/")
B.
from selenium import webdriver
chromedriver = r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.imdb.com/")
WebDriverException: Message:
'C:\Users\xxxx\AppData\Local\lxss\home\xxxx' executable may have wrong
permissions.
Then I did the following:
- Went to file Properties, under General, took off Read-only (Windows permissions)
- Went to file Properties, under Security and changed permissions to Full Control
- In the C:\Users\xxxxx\AppData\Local\lxss\home\xxxx file, I changed the permissions
using chmod 777 -R in my command prompt. Then I tried the following code:
from selenium import webdriver
import os
chromedriver = r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx\chromedriver.exe'
driver = webdriver.Chrome(os.path.join(os.getcwd(), 'chromedriver.exe'))
driver.get("https://www.imdb.com/")
WebDriverException: Message: Service /home/ariggs/chromedriver.exe
unexpectedly exited. Status code was: 1
I am stuck between these three error messages. Does anyone have another suggestion for a beginner?
You can actually start Windows executables from a linux subsystem as it is described here https://learn.microsoft.com/en-us/windows/wsl/interop.
But you have to keep in mind that Selenium and ChromeDriver communicate over a network connection. Actually chromedriver starts its own http server and Selenium sends requests and receives responses over http. (see https://sqa.stackexchange.com/questions/28358/how-does-chromedriver-exe-work-on-a-core-and-fundamental-level)
According to Microsoft, WSL and Windows share the same IP address and network connections via localhost are supported. But in your case there seems to be a problem during the startup.
You can start a remote webdriver on windows with Python and connect to that.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import subprocess
subprocess.run(["d:\\develop\\remotewebdriver.cmd", ""])
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://www.google.in/')
driver.close()
You need a windows script remotewebdriver.cmd for the remote webdriver that is called from Python:
SET JAVA_HOME=D:\develop\Java\jdk-11.0.2
d:
cd \develop
start D:\develop\Java\jdk-11.0.2\bin\java -Dwebdriver.chrome.driver=d:\develop\chromedriver.exe -jar selenium-server-standalone-3.141.59.jar
You have to adapt the path to your own environment. This setup works for me.
You can do this way.
Step 1:
Download chrome driver from this link (download the specific version as chrome):
http://chromedriver.chromium.org/downloads
Important: check your chrome version first. Go to help -> about Google Chrome, to see the version of your chrome.
Step 2:
After downloading extract and save the chromedriver file in a specific folder like, C:\selenium
go to environment variable and add a new path, C:\selenium
Step 3
Double click on the chromedriver application and then restart your command prompt. (If you are using conda environment.)

py2app selenium without firefox install

I am wondering if there is a way within py2app to include the Firefox browser, or if there is a way for Selenium to use Firefox without having to install Firefox on the host machine.
I have created an app with py2app that uses Selenium, however, I have Firefox installed on my machine, but not everyone that will receive the app will have Firefox installed. I am looking for a way to either include Firefox in the distribution or go around this.
Script will not run if Firefox is not preinstalled.
You can test your script with other browser, for example Chrome. If it works on Chrome also, then you can edit script like this:
from selenium.common.exceptions import WebDriverException
try:
driver = webdriver.Firefox()
except WebDriverException:
driver = webdriver.Chrome()
You can add same for few more browsers (IE, Opera, Safari...) to be sure that script will run on users machine

Categories

Resources