How to stop selenium from printing WebDriver manager startup logs? - python

When I'm launching a new selenium driver I get a message as:
====== WebDriver manager ======
Current chromium version is 90.0.4430
Get LATEST chromedriver version for 90.0.4430 chromium
Driver [/root/.wdm/drivers/chromedriver/linux64/90.0.4430.24/chromedriver] found in cache
I tryed using:
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
chrome_options.add_argument('log-level=2')
But none worked.
Is there a better way ?

To silent webdrivermanager-python logs and remove them from console, you can initialize the env variable WDM_LOG_LEVEL with 0 value before your selenium tests as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import os
os.environ['WDM_LOG_LEVEL'] = '0'
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")

according to documents:
just add below code into your files:
import os
os.environ['WDM_LOG'] = '0'
i have tried it with myself, working very well

The log-level that you are setting for chrome_options is completely separate from the logs that you are seeing from using the external library webdrivermanager for Python. That library will have its own way of disabling log messages (or at least it should). There are other Python libraries for managing WebDriver installs, such as SeleniumBase for example. Related, you might be able to change the Python logging level to hide that message, see Dynamically changing log level without restarting the application for details.

Are you using web driver manager? it looks like that is what is giving you logs (pip install webdriver-manager) . Im using selenium without web driver manager or adding any chrome options to remove logs , and not getting any logs printed.
also see :Turning off logging in Selenium (from Python)

This worked for me for webdriver_manager v3.8.3:
from webdriver_manager.core.logger import __logger as wdm_logger
wdm_logger.setLevel(logging.WARNING)

Related

Chrome browser closes after running selenium chrome webdriver

I'm currently learning Selenium 4.0 and have set up a basic script that will click a button on Python's website. I'm using a Chrome webdriver. But whenever I run my code, a chrome window opens to the Python website and then closes immediately. How do I keep it open?
The browser version and the webdriver version are the same, and I've even tried the Edge webdriver and reinstalling Chrome. I've even tried downloading a webdriver to my local directory, but that doesn't work either. Here's my current script:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()
After running, my terminal gives this message:
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org
Process finished with exit code 0
Well, it is the correct behavior as it does everything you told it to do correctly. Infact you're not recieving any errors. After having executed the code, Chrome Driver got killed because the Python app finishes its execution
If you want the Browser opened by the Driver to stay open use Chrome option and add detach
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

Chromedriver driver.get() working on Mac but does work on Windows (Chrome version 101.0.4951.54)

I am writing a program that uses selenium and chromedriver to load a page. The same code loads the page (nytimes.com) on my Windows computer but not on my Mac. On my Mac, it loads the webdriver with the blank data:, page but just stops and the console log just shows it waiting. I don't know why the driver does not get the page.
This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
opts = Options()
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://nytimes.com')
html = driver.page_source
This below is the last thing it shows in the console log. It just waits there after that with the blinking cursor.
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/me/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver]
found in cache
testing.py:14: DeprecationWarning: executable_path has been deprecated, please pass
in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
What could be the problem? I have a suspicion that it's the new version of chrome that I'm using but why would that change anything?
From the below message it seems you executed code before which installed older version of driver. Now when you trying to run code again , it detecting older version
in cache. Please check below path once.
Driver [/Users/me/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver]
found in cache

How can I use Firefox webdriver in pythonanywhere.com

I can use Chromedriver easyly in pythonanywhere.com but how can I use Firefox driver in pythonanywhere's python script ?
this is how I use chromedriver
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
browser.get("https://www.youtube.com/")
print("Done")
browser.quit()
PythonAnywhere dev here: unfortunately Firefox doesn't work well enough to be fully supported on PythonAnywhere now -- it's too resource-intensive. Chrome is more lightweight, so it's the only Selenium option we support.
(Interestingly, it used to be the other way around -- Firefox was more lightweight, and was the one that we supported. But the browsers have changed over time, so we have to change with them.)

launching chrome with extension running (python + selenium) how to keep browser open

I am currently trying to launch google chrome with a certain chrome extension using Selenium and Python. The script works partially, google chrome detects and launches with the extension I wish to add though after the browser launches it closes almost instantly (error code: "...selenium.common.exceptions.SessionNotCreatedException: Message: session not created"). How do I keep the browser open after chrome launches with the extension? The script does not read anything past the driver launch, "driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')"
code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
unpacked_extension_path = '/path/to/unpacked/extension/''
options = Options()
options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')
driver.get('example.com')
I have tried adding "chrome_options.add_experimental_option("detach", True)" and/or "global driver."Any help would be very appreciated!
screen-capture:
https://gyazo.com/52d6d6e6cfa61f5d8660204d773b5d03

Python, PhantomJS says I am not using headless?

my code is:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='driver/bin/phantomjs.exe')
driver.get("https://www.test.com")
print(driver.current_url)
It seems to run fine but before it runs I always get this error:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless
Why am I getting this error? I thought my PhantomJS was headless as it still works and no browser pops-up is this error save to ignore?
Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.
Here are the steps to use Chrome in headless mode:
download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/getting-started
extract it to a folder
add this folder to your PATH environment variable (if you don't do it, you will have to use webdriver.Chrome('/your/path/to/chromedriver') in the code below instead of webdriver.Chrome())
Then use it like this:
from selenium import webdriver
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/xhtml')
print(browser.current_url)
browser.quit()
More on how to use ChromeDriver
For the other options: here (also here and here)
In Selenium 3.8.1 PhantomJS marked as deprecated webdriver and recommend us to use either Chrome or Firefox in headless mode.
You could use this:
from selenium import webdriver
browser = webdriver.Chrome('chromedriver_path/chromedriver')
browser.get("https://www.test.com")
print(browser.current_url)
browser.quit()
Found an alternative you can add options.add_argument('headless') to chrome

Categories

Resources