How do I fix this error in selenium webdriver? - python

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.

Related

Python selenium headless start with extensions and xfvb

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.

launching chrome with extension running (python + selenium) how to keep browser open

I am currently trying to launch google chrome with a certain chrome extension using Selenium and Python. The script works partially, google chrome detects and launches with the extension I wish to add though after the browser launches it closes almost instantly (error code: "...selenium.common.exceptions.SessionNotCreatedException: Message: session not created"). How do I keep the browser open after chrome launches with the extension? The script does not read anything past the driver launch, "driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')"
code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
unpacked_extension_path = '/path/to/unpacked/extension/''
options = Options()
options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')
driver.get('example.com')
I have tried adding "chrome_options.add_experimental_option("detach", True)" and/or "global driver."Any help would be very appreciated!
screen-capture:
https://gyazo.com/52d6d6e6cfa61f5d8660204d773b5d03

Python, PhantomJS says I am not using headless?

my code is:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='driver/bin/phantomjs.exe')
driver.get("https://www.test.com")
print(driver.current_url)
It seems to run fine but before it runs I always get this error:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless
Why am I getting this error? I thought my PhantomJS was headless as it still works and no browser pops-up is this error save to ignore?
Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.
Here are the steps to use Chrome in headless mode:
download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/getting-started
extract it to a folder
add this folder to your PATH environment variable (if you don't do it, you will have to use webdriver.Chrome('/your/path/to/chromedriver') in the code below instead of webdriver.Chrome())
Then use it like this:
from selenium import webdriver
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/xhtml')
print(browser.current_url)
browser.quit()
More on how to use ChromeDriver
For the other options: here (also here and here)
In Selenium 3.8.1 PhantomJS marked as deprecated webdriver and recommend us to use either Chrome or Firefox in headless mode.
You could use this:
from selenium import webdriver
browser = webdriver.Chrome('chromedriver_path/chromedriver')
browser.get("https://www.test.com")
print(browser.current_url)
browser.quit()
Found an alternative you can add options.add_argument('headless') to chrome

Selenium-Python with chromium browser (windows)

I am trying to launch chromium browser with selenium python on windows 8.
Added binary_location as chromium binary location which is appdata.
But still chromedriver starts google chrome instead of chromium.
If I uninstall google chrome, then chromedriver by default launches chromium. But with chrome installed it always launches chrome regardless.
Does anyone have an idea on how to start chromium with selenium while chrome installed?
Please do not mark it as duplicate. The other one was about unix and solution provided to selenium java while this one is about windows and python.
Try this:
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = r"D:\.....\chrome.exe"
# This line defines your Chromium exe file location.
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.com/')
Worked for me.
I installed both Chrome and Chromium.
It starts the specified exe.
To start Chromium browser you can use the following code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location("C:\\path\\to\\chrome.exe") //path to chromium binary
options.add_argument("start-maximized")
options.add_argument("--disable-gpu-vsync") //only for Windows
options.add_argument("--remote-debugging-port=9222") //for MAC & Linux
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get('http://google.com/')
Note : Here you can find more details about Run Chromium with flags

Selenium Chromedriver not working in Python, Centos Cpanel Server

I installed selenium and chrome driver.
I then created the below code to run it:
from pyvirtualdisplay import Display
from selenium import webdriver
chromedriver = "/usr/bin/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get(url)
html = driver.page_source
driver.quit()
display.stop()
The code yields the following error: "'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home"
The path is correct. I have tried some variations of the code but they all yield the same message. My webhost claims you cant install selenium on a centos server that has cpanel. Is this true or is there something wrong with my code?
you might use OS.environ and chromeoptions

Categories

Resources