Page is not loaded in Selenium WebDriver using Python on Windows - python

I am using Python 3.5 on a Windows computer. When I run this code on my Mac it works perfect, no issues what so ever. But when I bring the code to my Windows computer it doesn't work.
Basically a blank page is shown instead of the home page. I don't get any error messages.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.google.com')
cookies = driver.get_cookies()
print(cookies)
Once I close the web browser I get this message in the shell:
"The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
From what I've been able to find online (most is for Java) it looks like I may need to setup a profile? Is this correct and could anyone help with this?

It looks like your client doesn't have the fix for the new switch to launch the gecko driver:
https://github.com/SeleniumHQ/selenium/commit/c76917839c868603c9ab494d8aa0e9d600515371
Make sure that you have the latest beta version installed (selenium-3.0.0b2) if you which to use the geckodriver v0.10.0 and above:
pip install -U selenium --pre
Note that you need the --pre flag to install the beta version.

Related

Chromium Webdriver with "--no-sandbox" is opening a fully transparent/invisible Chrome window

The relevant code is as follows:
# find the Chromium profile with website caches for the webdriver
chrome_options = Options()
profile_filepath = "user-data-dir=" + "/home/hephaestus/.config/chromium/Profile1"
chrome_options.add_argument(str(profile_filepath))
# put chromium into --no-sandbox mode as a workaround for "DevToolsActivePort file doesn't exist"
chrome_options.add_argument("--no-sandbox")
# start an automatic Chrome tab and go to embervision.live; wait for page to load
driver = webdriver.Chrome("./chromedriver", options=chrome_options)
When I run this Python code (and import the needed libraries), I get the screenshot below. Chromium that was opened with the above code is on the right, and is transparent and glitching out.
Desktop view with Chromium webdriver tab glitching out on the right
I am able to enter web addresses and interact with the page, but I just can't see any of it. I'm not sure why.
I deleted and re-downloaded Selenium and Chromium, to no avail. I had to add the "--no-sandbox" option because it was getting another error that said "DevToolsActivePort file doesn't exist".
I'm not sure what else is causing this issue.
So I found a solution that works for me!
Uninstall and reinstall Chromium completely. When reinstalling, check that your Chromium version matches with Selenium (which I didn't even know was a thing).
DO NOT run your Python code as a sudo user. I did "sudo python3 upload_image.py" and got the "DevToolsActivePort file doesn't exist" error. When I ran just "python3 upload_image.py", it did not raise the error.
Do not use the option "--no-sandbox" when running as a non-sudo user ("python 3 upload_image.py"). For some reason, the "--no-sandbox" option also broke my Chromium browser in the same transparent/infinite way as I posted above.
Hope this helps someone in the future!

Pycharm is running script headless successfully but terminal throwing error

Hello all,
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
dr = webdriver.Chrome(ChromeDriverManager().install(), options=options)
dr.get("url")
cWait = WebDriverWait(dr, 5)
usernameField = cWait.until(EC.presence_of_element_located((By.ID, "txtUserID")))
while running the above set of lines of code, when i click on pycharm run for this python file, it works in headless in good way but when I use
terminal and write code something like
python main.py
the console gives this output
====== WebDriver manager ======
Current google-chrome version is 92.0.4515
Get LATEST driver version for 92.0.4515
Driver [C:\Users\bc62700\.wdm\drivers\chromedriver\win32\92.0.4515.107\chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:53298/devtools/browser/b967343c-b07a-43a3-8d1f-d8b41e62e50d
[0906/071237.616:INFO:CONSOLE(19)] "Uncaught TypeError: Cannot read property 'txtUserID' of undefined", source: URL (19)
It is working absolutely fine with pycharm run button, but not with terminal
please help as because of this reason I am not able to make this as schedular event.
When you run it from within pycharm, it is using a specific python, which may be different from your system's default python. Check the run configuration in pycharm and see what the path is to the python version it is using, and try to execute it using the full path to that python, i.e. instead of python main.py it would be something like /path/to/your/project/venv/bin/python /path/to/your/project/main.py
If that's not it, try giving us a more complete explanation of your situation, so that someone can try to reproduce what's happening and investigate further.

My selenium script (in Python) for chrome runs but does nothing

vscode says it run but it does nothing. My pip is up to date and i installed selenium with the command prompt. i have a valid path (i think) for chromedriver. Here's my code:
from selenium import webdriver
chromedriver = "C:Users/P-Lou/Downloads/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("http:google.com")
The result is this:
[Running] python -u "w:\Python\Seletest\app.py"
[Done] exited with code=0 in 0.067 seconds
I tried this code by only changing the browser to firefox and removing the chromedriver = part and it worked fine, you might be running the browser in headless mode, thats why nothing pops up. You can change that using The Chromedriver.options(set_headless="false")
(not sure if that is the right code, you can check yourself here)
Presuming the ChromeDriver binary location being C:\Users\P-Lou\Downloads on your windows system you can use the following solution:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\P-Lou\Downloads\chromedriver.exe')
driver.get("http:google.com")

python selenium driver.quit() can not quit firefox browser if firefox_profile setted

This problem really confused me a lot.
I'm using python selenium do some automatic work. After the work done, i need to close browser(I must use firefox). And I know driver.close() is to close current window and driver.quit() will close all windows and exit browser. The problem is: it doesn't work for me if i am using python file.py to run my code, but work if I setup the driver in python console, here not work is to say it just close my url, but the firefox browser not exit. All above tests have setted firefox_profile.
More, i found if i don't set firefox_profile the first way to run my code also working. I think maybe it's a bug for firefox_profile. Wish someone to save my day.
My code is just like:
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', download_dir)
profile.set_preference(
'browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')
driver = webdriver.Firefox(
executable_path=gecko_dir, firefox_profile=profile)
driver.get(someurl)
driver.quit()# which will close my session, my url, my current window but not exit browser
the versions i'am using:
python 3.5.3
selenium 3.4.3
firefox 55.0.1
geckodriver 0.18.0
While we work with Selenium 3.5.0 with latest GeckoDriver v.0.18.0 and Mozilla Firefox 53.0, the browser window(s) initiated by the WebDriver instance is bound to get destroyed (killed) once you invoke the quit() on the WebDriver instance.
So the blank window which you are seeing may be a result of either some other user interactions or a dangling instance.
Recommendation:
You can consider the following steps to start a clean Test Execution:
Run CCleaner tool to wipe out all the leftovers from your previous executions.
Restart your system to start your Test Execution on a clean OS environment.
Keep the Test Environment isolated from other User Interactions.

Selenium webdriver seems not working properly with Firefox 49.0. Am I missing something?

I have gone through previous questions but did not find anyone else running into my issue.
This simple code hangs
from selenium import webdriver
d = webdriver.Firefox();
The browser gets launched, but it just hangs there. After sometime, it crashes and I get the error
selenium.common.exceptions.WebDriverException: Message: Can't load the profile.
Profile Dir: /tmp/tmpn_MQnf If you specified a log_file in the
FirefoxBinary constructor, check it for details.
I have Firefox49 on Ubuntu 14.04 LTS. I had selenium 2.53.6 and reading a previous post, I upgraded to selenium 3.0.0.b3. I also downloaded geckdriver and put it in /usr/bin
It looks like I was still running older version of selenium. But when I uninstalled that and installed selenium 3.0.0.b3, I see the following error -
selenium.‌​common.exceptions.We‌​bDriverException:
Message: Service geckodriver unexpectedly exited. Status code was: 2
What am I missing?
Try renaming the downloaded Gecko Driver to Wires and Set the Capabilities as mentioned Below.
System.setProperty("webdriver.gecko.driver", "//home//.....//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
Driver = new MarionetteDriver(capabilities);
I tried with the Above code on FireFox 49 on Linux Ubuntu 14.04 LTS... Code Works Fine For Me in Java..
Also try to downgrade the selenium WebDriver from Beta to 2.53 as Beta version is unstable..

Categories