I'm new to selenium just started learning it using freecodecamp and started using selenium downloaded selenium using the following command in python
pip install selenium
And I manually installed chrome web driver. Written the code as:
import os
from selenium import webdriver
os.environ["PATH"] =r"C:/seleniumDrivers" #where the chrome driver is installed
driver = webdriver.Chrome()
The following error occured when I run this:
line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be
in
PATH. Please see https://chromedriver.chromium.org/home
But if I modified code a bit:
import os
from selenium import webdriver
driver = webdriver.Chrome("C:/seleniumDrivers/chromedriver.exe)
A pop website appeared and closes immediately. Can anyone please suggest what should I do.
CodePudding user response:
In the newer selenium, including the webdriver is depreciated. Use ChromeDriverManager instead of referencing the path to chromedriver.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
CodePudding user response:
For the first issue
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
You have to point the PATH to the correct directory, which containing the chromedriver.exe
executable on the first level (i.e. you can see the executable when you open the path, it usually named bin
)
For the second issue
A pop website appeared and closes immediately. Can anyone please suggest what should I do.
It is because the code has finished (no further code to execute), so the program exits and closes the browser. You can try adding input()
after the last line to see if the browser remains open (until you press enter on the command line).
CodePudding user response:
You don't need to use the os module unless this is specific to your script, you can save the path as a variable. I'm using Selenium currently and this should work for you - you need to specify the "Service" now in the webdriver. Do the 2 pip installs first in terminal.
# pip install selenium #
# pip install webdriver-manager #
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
service = Service("C:/seleniumDrivers/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get(URL)