WebDriver crashes on IE - python

Suddenly my Python scripts on Selenium stop to launch because of following code and error:
from selenium import webdriver
dr = webdriver.Ie()*
*Actually new browser session opens on webdriver's default page but webdriver stop to respond so no other commands could be executed
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\me\AppData\Roaming\Python\Python34\site-packages\sele
nium\webdriver\ie\webdriver.py", line 57, in init
desired_capabilities=capabilities)
File "C:\Users\me\AppData\Roaming\Python\Python34\site-packages\sele
nium\webdriver\remote\webdriver.py", line 89, in init
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\me\AppData\Roaming\Python\Python34\site-packages\sele
nium\webdriver\remote\webdriver.py", line 138, in start_session
'desiredCapabilities': desired_capabilities,
File "C:\Users\me\AppData\Roaming\Python\Python34\site-packages\sele
nium\webdriver\remote\webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "C:\Users\me\AppData\Roaming\Python\Python34\site-packages\sele
nium\webdriver\remote\errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi
ng Internet Explorer. Could not get document from window handle
IE version 11
IeWebDriverServer version 2.35 x64
Why this trace appeared? Maybe someone faced the same issue.. Any help will be appreciated
UPDATED
Resolved. Current IeWebDriverServer version replaced with v2.48

Check the following:
You are using the same security settings, for all the zones.
You are using the correct driver. Use the 32 bit when in doubt.
Make sure your registry settings are correct.
Zoom level is 100%.
Source: I am the creator of Germanium, an opensource testing API, and I support IE :)

Related

Python Selenium Chrome - Message: unknown error: cannot parse internal JSON template: Line: 1, column: 1, Unexpected token

This code works on two PCs but throws an error on another one.
webdriver_path: str = os.environ.get("WEBDRIVER_PATH")
chrome_user_data_dir: str = os.environ.get("CHROME_USER_DATA_PATH")
chrome_profile_dir: str = os.environ.get("CHROME_PROFILE_DIR")
options: webdriver.ChromeOptions = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={chrome_user_data_dir}")
options.add_argument(f"profile-directory={chrome_profile_dir}")
driver: webdriver.Chrome = webdriver.Chrome(executable_path=webdriver_path, options=options)
I get the following error:
File "C:\Users\Pavel\Desktop\Saka-Automation\etsy_create_listing.py", line 100, in <module>
main()
File "C:\Users\Pavel\Desktop\Saka-Automation\etsy_create_listing.py", line 27, in main
driver: webdriver.Chrome = webdriver.Chrome(executable_path=webdriver_path, options=options)
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 93, in __init__
RemoteWebDriver.__init__(
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 357, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:\Users\Pavel\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot parse internal JSON template: Line: 1, column: 1, Unexpected token.
I saw the same question asked with very few answers that didn't solve the problem. Tried deleting /Preferences and /Local State in the chrome user profile directory. Tried reinstalling chrome and chrome webdriver. Tried with different selenium versions. Nothing seems to work for now.
Any ideas or solutions that you used for this problem would be very helpful. Thanks in advance.
I managed to get it up and running again! The solution that worked for me was deleting C:\Users\USER\AppData\Local\Google\Chrome folder and reinstalling Chrome. If you run into this problem, give it a try.

Python + Selenium(Chrome): Expansion timeout on slow computers

I'm trying to load an extension(Metamask) into chrome to do some automated stuff. It works just fine on on my gaming grade rig, but both my laptop as well as my technical outdated homeserver pc timeout:
opt = webdriver.ChromeOptions()
opt.binary_location = self.chrome_exe_location
opt.add_argument('--log-level=3')
opt.add_argument('--start-maximized')
opt.add_experimental_option('excludeSwitches', ['enable-logging'])
opt.add_extension(os.path.join(os.getcwd(), '10.9.3_0.crx'))
self.driver = webdriver.Chrome(self.chromedriver_exe_location, options=opt, service_args='')
Traceback (most recent call last): File
"C:\OneDrive\CloudDesktop\Python\main.py", line 968, in
bot() File "C:\OneDrive\CloudDesktop\Python\main.py", line 69, in
init
self.chrome_initialize() File "C:\OneDrive\CloudDesktop\Python\main.py", line 118, in
chrome_initialize
self.driver = webdriver.Chrome(self.chromedriver_exe_location, options=opt, service_args='') File
"C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File
"C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 93, in init
RemoteWebDriver.init( File "C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 269, in init
self.start_session(capabilities, browser_profile) File "C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 360, in start_session
response = self.execute(Command.NEW_SESSION, parameters) File "C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 425, in execute
self.error_handler.check_response(response) File "C:\OneDrive\CloudDesktop\Python\vEnv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error:
failed to wait for extension background page to load:
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.html
from timeout: Timed out receiving message from renderer: 10.000
Chrome and chromedriver versions 98.0.4758.102 64bit on all computers
Python versions 3.10.1(one pc working and one desn't) and 3.10.2(not
working)
OS: Win 10(none working) and 11(working) all updated
CPU:5600x(working), Snapdragon 8cx(not working),Intel Celeron J3455(not working)
I guess this timeout occurs because selenium does not wait until the expansion is fully loaded or transferred. The .crx is about 18MB in size and I'd say the computer on which it's working takes about 5 secs and both other ones about 30 secs.
I already tinkered with selenium timeouts, but I found it's a lost cause, because I really can just set these parameters after the selenium object is created, which causes this timeout in the first place.
Does anyone of you guys know a way how selenium allows the startup process some more time?
Thanks for your help!
In the official Google Chrome support for chromedriver, there were some suggestions to use options.add_experimental_option('extensionLoadTimeout', 60000).
The time specified is in milliseconds, so 60 thousand corresponds to a timeout of one minute. This fixes timeout problems for slow PCs.
I found a workaround:
Download extension in a normal use instance of chrome.
chrome://version/ copy profile path
e.g. C:\Users\pcuser\AppData\Local\Google\Chrome\User Data\Default
Python code:
profile_path = r'C:\\Users\\pcuser\\AppData\\Local\\Google\\Chrome\\User Data'
opt.add_argument('--user-data-dir={}'.format(profile_path)) # profile path
opt.add_argument('--profile-directory={}'.format('DEFAULT')) #profile name
self.driver = webdriver.Chrome(self.chromedriver_exe_location, options=opt, service_args='')
self.driver.set_page_load_timeout(60) # adapt as you see fit
self.driver.set_script_timeout(60) # adapt as you see fit
self.driver.implicitly_wait(60) # adapt as you see fit

How do I resolve this Selenium exception on a Mac thats says "chrome not reachable"?

I'm trying to learn how to automate web processes using Selenium and hopefully be able to build robust web scrapers and stuff. So, I just finished installing Pycharm and Selenium, and I am just trying to run a simple snippet of code that opens a web page in chrome, nothing too fancy. My code is as follows (it's in Python of course)
from selenium import webdriver
PATH = "/Users/Kudzie/Tools/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get('https://orbitrevolution.tech')
So , when I run this code, I get this exception, and I have no idea how to solve this, I've tried reading past variations of this question but i didn't seem to get a solution that works. the exception is as follows
Traceback (most recent call last):
File "/Users/Kudzie/PycharmProjects/Web Scraper/main.py", line 5, in <module>
driver = webdriver.Chrome(PATH)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
Process finished with exit code 1
Try replacing this:
driver = webdriver.Chrome(PATH)
With this:
driver = webdriver.Chrome(executable_path=PATH)

about selenium, not knowing what errors are

Here's my code and the error Python provides.
I'm a beginner using Python 3.6. Can anyone helps find out what's wrong? Many thanks.
#!/usr/bin/env python
#coding: utf-8
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe')
browser = webdriver.Chrome()
browser.get('http://www.bing.com/')
Traceback:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\cxy61.com - html\python spider\001 selenium import.py", line 8, in <module>
driver = webdriver.Chrome('C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe')
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 140, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: session not created exception: Chrome version must be >= 58.0.3029.0
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.14393 x86_64)
You have to take care of a couple of things as follows:
As you are using chromedriver v2.31 the error clearly states Chrome version must be >= 58.0.3029.0. So you need to bump up your Google Chrome version to v58.0 or above.
If you want to specify the absolute path of the chromedriver within single quotes '...' then you have to provide single forward slashes \ only.
If you are assigning the webdriver instance to the driver, you have to use the driver instance only to open any url
Here is your own code with the above mentioned changes:
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('http://www.bing.com/')

Can't open IE using selenium in python

I am running on a Windows 10 machine, Internet Explorer 11, python 3.6, selenium 3.4.3 with IEDriverServer 3.5. I am trying to open up IE using the following code.
from selenium import webdriver
import os
driverLocation = "C:\\Users\\JD\\PycharmProjects\\Lib\\IEDriverServer.exe"
os.environ["webdriver.ie.driver"] = driverLocation
driver = webdriver.Ie(driverLocation)
google = "https://google.com"
driver.get(google)
The output:
Traceback (most recent call last):
File "C:/Users/J/PycharmProjects/Automation/IE_Test.py", line 7, in <module>
driver = webdriver.Ie(driverLocation)
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\ie\webdriver.py", line 57, in __init__
desired_capabilities=capabilities)
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Invalid capabilities in alwaysMatch: unknown capability named platform
Any help would be greatly appreciated thanks.
UPDATE:
I added this to my previous code,
capabilities = DesiredCapabilities.INTERNETEXPLORER
print(capabilities["platform"])
print(capabilities["browserName"])
OUTPUT:
WINDOWS
internet explorer
File "C:\Users\JD\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Invalid capabilities in alwaysMatch: unknown capability named platform
UPDATE:
I have also tried setting the capabilities but still recieve the same error: "unknown capabilities named platform
caps = DesiredCapabilities.INTERNETEXPLORER.copy()
caps["platform"] = "WINDOWS"
caps["browserName"] = "internet explorer"
caps["requireWindowFocus"] = True
browser = webdriver.Ie(capabilities=caps,
executable_path="C:\\Users\\JD\\PycharmProjects\\Lib\\IEDriverServer.exe")
browser.get("https://www.facebook.com/")
I had the same problem for couple of days.
My workaround for this was to delete platform and version keys from capabilities dictionary
Example:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#create capabilities
capabilities = DesiredCapabilities.INTERNETEXPLORER
#delete platform and version keys
capabilities.pop("platform", None)
capabilities.pop("version", None)
#start an instance of IE
driver = webdriver.Ie(executable_path="C:\\your\\path\\to\\IEDriverServer.exe", capabilities=capabilities)
driver.get("https://www.google.com/")
My guess, so far, is that this error happens because w3c_caps are passed as the only right capabilities. You can see that in the Traceback:
response = self.execute(Command.NEW_SESSION, parameters)
when you click on it you will see that:
w3c_caps["alwaysMatch"].update(capabilities)
As you can see here _W3C_CAPABILITY_NAMES hold different values than the ones we are passing.
We are passing "WINDOWS" as "platform", while _W3C_CAPABILITY_NAMES has "platformName" and accepts only small caps. Same goes for "version" key.
So we were adding capabilities that are not recognized.
This workaround is by no means perfect, and I was able to start IE in selenium java without deleting some of the capabilities.
EDIT: Another solution can be found here in Grimlek comment, which essentially says that you should delete "capabilities": w3c_caps from start_session(self, capabilities, browser_profile=None) (from remote\webdriver.py). Code looks like this:
w3c_caps["alwaysMatch"].update(capabilities)
parameters = {"capabilities": w3c_caps,
"desiredCapabilities": capabilities}
Then you would not need to delete keys from capabilities.
ANOTHER EDIT: I have just updated my selenium-python from 3.4.3 to 3.5.0 and there's no longer need for messing with capabilities.

Categories

Resources