I am learning Python to tried to do an extraction of data from another website. However, I wrote a simple code to try to open a Chrome browser window and display google on my web browser.
I have seen in other videos that it is only needed to write the following code to get this to work:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
However, when I try to run it on my PyCharm, I got plenty of error lines. The same happen if I try to run it through my command prompt (both stating the same error - pictures below). I do not know what I am missing, but I should mention that I have downloaded the packages pip, selenium, selenium-chromedriver and I have also downloaded ChromeDriver separetely from the website https://chromedriver.chromium.org/
Could anyone please guide me through this issue? Thanks a lot everyone for your precious help and your time!
Kind regards,
Salvador
ChromeDriver_executable_location
CodePudding user response:
Please post the errors so we can see what it is tell you is wrong.
I am also new to selenium and learning python.
First, make sure your Chromedriver is located in the same folder as your script. Second, make sure your version of Chrome and Chromedriver are compatible.
Also, when the script is finish it will close the browser immediately.
I ran your posted code and it works for me. Did you have any errors when installing the packages?
CodePudding user response:
I'm not sure why you get an error, but I know a workaround that might work for you.
Instead of using the webdriver file, you can use a service to install it for you, like this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
You need to install the webdriver_manager library first using pip
Hope this helps
CodePudding user response:
You could place the Chromedriver
in system path
and try to execute.
- Go to Advanced System Settings.
- Click on Environment variables.
- Select Path variable and click on Edit.
- Add the
Chromedriver
folder path. Suppose the path for chromedriver.exe file is -C:/project/Chromedriver/chromedriver.exe
, placeC:/project/Chromedriver
in thePath
variable.
Restart the computer and then try to execute below lines:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")