selenium: chrome failed to start when using options - python

I am trying to use selenium with chrome web driver. When I run it without the options it works fine:
>>>driver = webdriver.Chrome(executable_path=path)
but when I add options (loaded profile) like this:
>>> options = webdriver.ChromeOptions()
>>> options.add_argument('--user-data-dir=%s' % chromeProfile)
>>> driver = webdriver.Chrome(executable_path=path,chrome_options=options)
it starts the browser window, which seems alive, but the command hangs until it finally declares:
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'unknown error: Chrome failed to start: exited normally\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)'
what could be causing this problem and how can I go about debugging it?
Thanks

OK - figured it out. The problem seems to be that one profile cant be opened while trying to connect a webdriver to it. I copied the default profile to a new location, and now it works fine. I'll leave this here in case this is useful to anyone.

Related

Selenium cannot interact with an already open Google Chrome window when running on a remote server

I'm trying to connect selenium to an already open Google Chrome window in the following way:
options.add_experimental_option('debuggerAddress', 'localhost:9112')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-gpu')
options.add_argument('enable-automation')
self.d = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe', options=options)
By first calling Google Chrome from the command line:
chrome.exe --remote-debugging-port=9112 --user-data-dir="C:\selenum\AutomationProfile"
At first it's working fine, driver does his thing, but as soon as I logged out of the server and, of course, can't see browser window, Selenium also stops seeing it and raising errors
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=105.0.5195.102)
no such window: target window already closed from unknown error: web view not found (Session info: chrome=105.0.5195.102)
The interesting thing is that when I switch back to the remote machine, the browser loads and continues to work.
I also tried to add
options.add_experimental_option("detach", True)
But it didn't solve the problem. When I run selenium in normal way without command line, debugging and other stuff then it works stable, but I need to handle an already opened Google Chrome window.

Chrome Driver crashes in headless

I am making a bot to scrap me some information from the web via chromedriver. But because this information is sometimes limited to an account (like instagram insights) i need to use several UserData-Folders to save all the logins etc for the chromedriver. To save some memory I made a function "init" which initializes a chromedriver window with the desired UserData-Folder.
the function:
def init(userdata):
global driver
warnings.filterwarnings("ignore", category=DeprecationWarning)
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("window-size=fullscreen")
option.add_argument("--disable-extensions")
option.add_argument("--log-level=3")
option.add_argument("--headless")
option.add_argument(r'--user-data-dir=dir\to\UserData' + str(userdata))
option.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
driver = webdriver.Chrome(chrome_options=option, executable_path='C:\Windows\chromedriver.exe')
when running the code without headless, it works like a charm. However when using headless it gives the following error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: was killed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
chrome_options have been deprecated for quite some time now. Instead you have to use options as follows:
driver = webdriver.Chrome(options=option, executable_path='C:\Windows\chromedriver.exe')

Getting error while opening a page using selenium webdriver [duplicate]

When I'm executing this code with Selenium using Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
The error occurred:
Traceback (most recent call last):
File "/Users/qa/Documents/Python/try.py", line 4, in <module>
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.44.609545 (c2f88692e98ce7233d2df7c724465ecacfe74df5),platform=Mac OS X 10.13.6 x86_64)
Can someone help me? Thanks.
I had a similar error, first getting the error message:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn't exist)
This was solved by adding options.add_argument("--remote-debugging-port=9230") to the ChromeOptions(). And the program runs once and I gained the same error message as above:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=89.0.4389.114)
The problem here was that the chrome process does not close properly in the program so the process is still active on the debugging-port. To solve this problem close the active port sudo kill -9 $(sudo lsof -t -i:9230) and simply add the following lines to the end of the code:
driver.stop_client()
driver.close()
driver.quit()
Since I didn't find this answer anywhere, I hope it helps someone.
If you have options.add_argument("--remote-debugging-port=9222") change this to options.add_argument("--remote-debugging-port=9230")
or just simply Adding options.add_argument("--remote-debugging-port=9230") fixed in my case.
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
You need to consider a fact:
As you are using Mac OS X the Key executable_path must be supported with a Value as :
'/Users/qa/Documents/Python/chromedriver'
So line will be:
driver = webdriver.Chrome(executable_path='/Users/qa/Documents/Python/chromedriver')
Note: The path itself is a raw path so you don't need to add the switch r and drop it.
Additionally, ensure that /etc/hosts on your system contains the following entry :
127.0.0.1 localhost.localdomain localhost
#or
127.0.0.1 localhost loopback
I ran into this same issue on a Windows 10 machine. What I had to do to resolve the issue was to open the Task Manager and exit all Python.exe processes, along with all Chrome.exe processes. After doing this,
I am facing the same error and add the close code solve the problem, the code finnaly block look like this:
#staticmethod
def fetch_music_download_url(music_name: str):
chrome_driver_service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--remote-debugging-port=9230")
driver = webdriver.Chrome(service=chrome_driver_service, options=chrome_options)
try:
driver.maximize_window()
driver.get('http://example.cn/music/?page=audioPage&type=migu&name=' + music_name)
driver.implicitly_wait(5)
// do some logic
except Exception as e:
logger.error(e)
finally:
// add the close logic
driver.stop_client()
driver.close()
driver.quit()
chrome_driver_service.stop()
the key is you should close the chrome service after using it by add chrome_driver_service.stop().hope this code could help other people facing the same issue.
This worked for me on WINDOWS OS
from selenium import webdriver
import time
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
driver.get(url)
time.sleep(3)
page = driver.page_source
driver.quit()
soup = BeautifulSoup(page, 'html.parser')
Hope you'd find this useful. I also used it more comprehensively here.

Is it possible to use `element.click()` on Selenium with Chrome even on headless mode?

I'm writing a script to download a set of files on a website using Selenium and its Chrome driver in Python, but I find it disgusting to see the browser opens and gets a focus whenever the program opens a new tab or clicks on the download button.
So I rather want to use headless mode. But then, the element always gets me the following error:
WebDriverException: Message: unknown error: Element ... is not clickable at point (435, 575). Other element would receive the click: ...
(Session info: headless chrome=66.0.3359.139)
(Driver info: chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85),platform=Mac OS X 10.13.4 x86_64)
This error once happened in my non-headless mode, but since I changed the size of the browser by driver.maximize_window(), the error had gone out. This may make it clear the button must be visible on the screen, in order for it to be pushed on Chrome.
But in the headless mode, it might be true that the button is hidden and that's way the element is always unclickable, leading to the error. So it there any way to make the button clickable even on the headless mode?
The relevant code is the following, and the last line causes the error:
def login(driver, url, username, password):
driver.get(url)
uname = driver.find_element_by_name("login")
uname.send_keys(username)
passw = driver.find_element_by_name("password")
passw.send_keys(password)
submit_button = driver.find_element_by_class_name("button")
action = webdriver.ActionChains(driver)
action.move_to_element(submit_button)
submit_button.click()
but since I changed the size of the browser by driver.maximize_window()
I faced this issue once when I was working with headless Chrome in Jenkins. The issue was that the browser, in the headless mode didn't open in full resolution. So I added the line to open the browser in specific resolution using
driver.set_window_size(1440, 900)
And then passing the url and getting everything . You can try this approach once to see if this is the issue.
I met this problem too. I just fix it like this:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
Short answer: Yes.
But there is one difference using clicks in headless and non-headless mode.
For non-HEADLESS mode, if you are doing clicks in chromedriver, and at the same time, you perform clicks somewhere on your screen by yourself, then this may interfere with your Selenium automation. This won't be a problem in HEADLESS mode.

Selenium Chromedriver Python - 'failed to wait for extension background page to load' when loading ModHeader extension using Tor proxy

This bug occurred out of nowhere in a program that previously functioned perfectly. I'm absolutely certain I have not done any changes on the program since it's functionality was perfect, so this is by far the weirdest and most frustrating bug I have ever experienced.
This is the code I'm trying to execute:
chrome_options.add_extension('C:\\chromedriver\\ModHeader_v (1).crx')
chrome_options.add_argument('--proxy-server=socks5://' + '127.0.0.1:9150')
driver = webdriver.Chrome('C:\\chromedriver\\chromedriver.exe', chrome_options = chrome_options)
Which raises the following error:
selenium.common.exceptions.WebDriverException: Message: unknown
error: failed to wait for extension background page to load:
chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html
from timeout: Timed out receiving message from renderer: 10.000
(Driver info: chromedriver=2.24.417431
(9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT
10.0.15063 x86_64)
I assume that a change within the Tor browser is causing this bug. There is no other potential cause.
I ran into this same error message. Downloading the latest version of chromedriver fixed it for me. (New version released 19 days ago: https://sites.google.com/a/chromium.org/chromedriver/downloads)
Try to use Seleniumwire, for example:
from seleniumwire import webdriver
def set_chrome_driver():
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--disable-infobars")
options.add_argument("--no-proxy-server")
driver = webdriver.Chrome(executable_path=r'C:\Automation_package\chromedriver.exe')
driver.get('http://172.1.1.1:5000/path/api/')
driver.header_overrides = {"iv-user": "Admin", "iv-groups": "SuperAdmin", "iv-roles": "Viewers",}
driver.get('http://172.1.1.1:5000/path/api/')

Categories

Resources