Python Selenium Chrome Webdrives closes unexpectedly after the execution of program - python

I have updated the version of chrome and accordingly webdriver. The chrome closes automatically after the execution of the last line. It was a simple code to open a chrome driver and click the links. After I run in command prompt it generated the error message "failed to load pepper module from internal-not-yet-present (error 126)".
Due to which the Pycharm generates the error "Oserror:[winerror 6] The handle is invalid"
Can anyone please suggest the solution for it?

I've experienced this (quite) a while ago. In my case an Adobe Flash Player update was required for Chrome: in chrome://components find the Adobe Flash Player entry and check for updates.
Alternatively, disable extensions:
options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options)

Related

All my webdrivers aren't working with Selenium, what can I do?

Recently all webdrivers that I try to use don't work correctly, and when I run the same code on another computer it works fine. The problems I get:
Chrome and Firefox open but stuck at data and then 'selenium.common.exceptions.WebDriverException: Message: chrome not reachable' for example;
Ms Edge won't even open and gives no error;
Already tried: installing and re-installing Selenium, Python, Pycharm, webdrivers of different versions, changed the PATH location, but the problem seems to be in my computer.
Simple code that I am trying to execute:
from selenium import webdriver
driver = webdriver.Chrome() #this is the function that isn't working
driver.get('https://www.google.com/')
Any suggestions?
Edit: After two weeks trying to figure it out this issue, the only solution that I found was to reinstall my Windows and all my applications again. Now everything is working as it should, so if anyone else has this problem this is my suggestion.
Thank you all!
I don't know if this will help, but I believe that you need to specify the location of the ChromeDriver or Google Chrome .exe location before you call your WebDriver. Once you specify the location of your ChromeDriver then you should be able to use it at your disposal.
Sample Code For You
from selenium import webdriver
def get_chrome_driver():
"""This sets up our Chrome Driver and returns it as an object"""
path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
# Browser is displayed in a custom window size
chrome_options.add_argument("window-size=1500,1000")
return webdriver.Chrome(executable_path = path_to_chrome,
options = chrome_options)
# Gets our chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://www.bbc.com/news/world")
chrome_driver.quit()
chrome_driver.service.stop()
when you Intialize the webdriver its always recomended to specify the path of the respective browser like ChromeDriver for Chrome browser and GeckoDriver for Mozilla Firefox.
Or
You can invoke WebDriver manager at the point which would handle this.
Link: https://snyk.io/advisor/python/webdriver-manager
Link: https://github.com/SergeyPirogov/webdriver_manager
After two weeks trying to figure it out this issue, the only solution that I found was to reinstall my Windows and all my applications again. Now everything is working as it should, so if anyone else has this problem this is my suggestion.
Thank you all!

I am using Google Chrome with Selenium and while going on Amazon, it detects it and Blocks everything. Can I bypass this?

This is tied to my other question here: Selenium cant seem to find Amazon "Pre-order now" button. Python
It worked for a bit but now when I run the code, it detects the robot and shows me the DOG image even after I enter in the captcha text. Please help.
I am not sure if this will help, But I would suggest you to open browser every time in incognito mode.
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(executable_path = driver_path, options = options)
I think I found the solution.
I deleted the Chromdriver.exe from where I had it downloaded and that fixed everything.

"This is the initial start page for the WebDriver server" - Unable to resolve in Selenium/Python program using IE 11

After running my Selenium/Python program, browser opened with below message:
This is the initial start page for the WebDriver server
I have done below steps to resolve this:
In IE Options -> Security tab, Enable Protected Mode check box is ticked OFF in all zones: Internet,
Local Intranet, Trusted sites and Restricted sites. Also, in Advanced tab -> Security, ticked OFF the
check box: "Enable Enhanced Protected Mode" (Also, I tried with enabling this Protected Mode in all
zones and in Advanced tab too).
My IEdriver (version 3.1.4) and Selenium web driver (version 3.1.4) are compatible (both are on same
version)
I tried above two, still I am getting the same message.
I have added below content to ignore Protected mode:
caps = DesiredCapabilities.INTERNETEXPLORER
caps['ignoreProtectedModeSettings'] = True
driver = webdriver.Ie(executable_path='C:/Selenium/Drivers/IEDriverServer.exe',capabilities=caps)
Still, I am getting the same message after adding above code.
Any ideas? Please help.
This is as per design. When IEDriverServer.exe opens a new a new Browsing Context i.e. Internet Explorer browsing session it navigates first to this page.
Browser Snapshot
Once you initialize the browser through the line:
driver = webdriver.Ie(executable_path='C:/Selenium/Drivers/IEDriverServer.exe',capabilities=caps)
next you can invoke the get() command to access any url. As an example:
driver.get('https://www.google.com/')
Additional Consideration
Additionally you need to:
Upgrade Selenium to current levels Version 3.141.59.
Upgrade IEDriverServer to latest IEDriverServer v3.150.1 level.
Note: As per best practices as Selenium Client and InternetExplorerDriver are released in sync and you must try to use both the binaries from the same major release.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Execute your #Test.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

Python Selenium Headless Chromedriver taking all white screenshots and can't locate any element?

Im setting a script up to flush the cache of a certain application, When running chromedriver not headless all xpath locators work fine. I set up the chrome options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("window-size=1920,1080")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get('link here')
driver.save_screenshot('file.png')
link here represents the certain link I will be going to flush the cache.
When running headless I get an unable to locate element error and the screenshots will come back as a blank page. The complete opposite when chrome is not run headless. I'm at a roadblock right now, any help or suggestions would be appreciated.
I've tried setting the windows size as well as changing the path of the chromedriver.

Selenium headless not working with custom profile selection

Below is the code I'm using - I've attempted to make it as concise as possible.
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import bs4
options=Options()
#options.add_argument('--headless') # Works while not headless?
options.add_argument('--disable-gpu') # Last I checked this was necessary.
options.add_argument("--user-data-dir=profiles\\") #Keeps login data
options.add_argument("--profile-directory=Profile 1")
driver=webdriver.Chrome(chrome_options=options)
driver.get("http://www.google.com")
html=driver.page_source
soup=bs4.BeautifulSoup(html, "html.parser")
print(soup)
The main problem stems from the --user-data-dir and --profile-directory parameter. In my test example a custom chrome profile (usually found at C:\Users\User\AppData\Local\Google\Chrome\User Data) is in the current directory to keep it separate from any currently running chrome sessions.
If the --headless parameter is enabled while using the above parameters the driver hangs (the CMD stays and does not produce output on the Python command line). However, when not enabled the window pops open and performs as expected.
However, --headless does work while using any profile within the above default directory.
This is the console output;
[0120/222514.611:ERROR:gpu_process_transport_factory.cc(967)] Lost UI shared context.
DevTools listening on ws://127.0.0.1:59961/devtools/browser/ee317ed6-93c7-47c2-b26d-63647980ba0d
[0120/222514.619:ERROR:devtools_http_handler.cc(289)] Error writing DevTools active port to file
[0120/222514.624:ERROR:cache_util_win.cc(19)] Unable to move the cache: 0
[0120/222514.625:ERROR:cache_util.cc(140)] Unable to move cache folder profiles\Default\GPUCache to profiles\Default\old_GPUCache_000
[0120/222514.625:ERROR:disk_cache.cc(184)] Unable to create cache
[0120/222514.625:ERROR:shader_disk_cache.cc(622)] Shader Cache Creation failed: -2
So, it looks like somewhere Chromium is assuming I'm using the Default profile when in fact I specify I am not.
Does anyone have any suggestions? Thanks in advance!
I was able to your code to run with the following option added.
options.add_argument('--remote-debugging-port=45447')
Without it the code was hanging for around a minute before the following error was thrown:
WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Comment excerpt which pointed me to a solution:
When you send --remote-debugging-port=0 to Chrome, devtools chooses
its own port and writes it to chrome::DIR_USER_DATA as a file
named "DevToolsActivePort".
Basically once the port is set to something other than the default of 0 the DevToolsActivePort file does not need to be checked.
The full comment and bug are here: chromedriver bug
Hope this helps!
I solved this error by closing the command prompt and reopening it in administrator mode (right-click on the cmd.exe and click Run as administrator).
Nothing else worked.

Categories

Resources