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)
Related
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
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)
For instance I have the following code :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
d = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)
d.get('https://www.google.nl/')
I am running this code from wsl on a jupyter notebook.
It executes but I want to be able to show the url that is opened in a chrome web browser.
for example, if I run the code :
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/SoumyaPandey/Desktop/Galytix/Data-Ingestion-Cell/codes/chromedriver.exe')
driver.get('https://www.google.nl/')
driver.maximize_window()
locally on spyder, it opens a new google chrome window saying that chrome is controlled by automated test software. I want to do the same in jupyter notebook :
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
I am trying to automation some action on a website using Python and Selenium, this is the sample code I am trying to run, from the Mozilla website for running the Firefox webdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "/usr/bin/firefox-aurora"
driver = webdriver.Firefox(capabilities=caps)
driver.get("https://www.google.com")
driver.quit()
When running this code, the Firefox instance opens normally, and I can even use the instance as if I just opened Firefox normally, but the code 'stops' executing at the driver = webdriver.Firefox(capabilities=caps) line, I tried debugging the code with no luck, the whole execution seems to just stop at this line, and nothing after it is reached!
I am running Python3.5, Selenium version 2.53.6, I have the 'Wires' executable at the /usr/local/bin which is in the environment's PATH, I also have Firefox Aurora version 49.0a2 running on ArchLinux.
Thanks in advance.
[Update:]
I managed to get it to work after all using Firefox 46 (the normal version).