Pycharm is running script headless successfully but terminal throwing error - python

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.

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!

learn version of chrome browser via python

I am writing a script by using selenium. My problem is when the chrome has been automatically updated, my script is not working. So, my solution is learning the web chrome version (not driver) at the beginning and run the related chrome driver. So on my desktop I will keep all versions and run the correct one. But I could not find a solution to get the version of chrome. I will kindly appreciate the helps! Thanks in advance!
Try this.
import subprocess
output = subprocess.check_output(
r'wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
shell=True
)
print(output.decode('utf-8').strip())
output
Version=79.0.3945.117

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.

Python Selenium Chrome issue in Windows: "KLIB_SelfTest return : KLR_OK"

I made a PyQt program which runs Chrome web driver via Selenium.
It works normally in Mac or Linux, but it prints a weird line in console when I run this program on Windows, as below.
KLIB_SelfTest return : KLR_OK
The log itself is not a big deal, but when it comes with Pyinstaller, it produces some error when compiled by --noconsole option. When I remove that option it works fine, but I want the console window not to be showed.
Moreover, I'm really curious about which part of the Chrome driver produces that log. I searched almost every information as far as I can, but I couldn't find anything related with the log.
Thanks in advance for your help.

Page is not loaded in Selenium WebDriver using Python on Windows

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.

Categories