[英]Why isn't my selenium working in python script?
I'm having some issues with selenium in my python script.我的 python 脚本中的 selenium 有一些问题。
import time
from selenium import webdriver
browser = webdriver.Chrome(path/to/chromedriver)
After executing the script the terminal is just outputting:执行脚本后,终端只是输出:
SyntaxError: invalid syntax
the problem is in third line问题在第三行
browser = webdriver.Chrome(path/to/chromedriver)
and I don't really know what to do.我真的不知道该怎么做。 I'm pretty sure that the chrome driver path is correct tho.
我很确定 chrome 驱动程序路径是正确的。
Should be, assuming you are on windows应该是,假设你在 windows
browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
Or或者
browser = webdriver.Chrome(executable_path="C:\\path\\to\\chromedriver.exe")
Webdriver download网络驱动下载
https://sites.google.com/a/chromium.org/chromedriver/downloads https://sites.google.com/a/chromium.org/chromedriver/downloads
You can also download and install the chromedriver binary您还可以下载并安装chromedriver 二进制文件
Just import chromedriver_binary
.只需导入
chromedriver_binary
。 This will add the executable to your PATH so it will be found.这会将可执行文件添加到您的 PATH 中,以便找到它。
You can also get the absolute filename of the binary with chromedriver_binary.chromedriver_filename.
您还可以使用
chromedriver_binary.chromedriver_filename.
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
driver = webdriver.Chrome()
For Windows:对于 Windows:
You have to put the path as a string in quotes "<path to chromdriver>"
and would have to use r
(for raw) before the string if it only includes single \
eg r"C:\Users\user\..."
else you don't need it if you path is like "C:\\Users\\user..."
.您必须将路径作为字符串放在引号
"<path to chromdriver>"
r
,并且如果它只包含单个\
例如r"C:\Users\user\..."
否则,如果您的路径类似于"C:\\Users\\user..."
,则不需要它。
Do:做:
import time
from selenium import webdriver
browser = webdriver.Chrome(r"<path to chromedriver>")
where "path to chromedriver" is the path to the chromedriver eg "C:\Users\<user>\Downloads\chromedriver_win32\chromedriver.exe"
.其中“chromedriver 的路径”是 chromedriver 的路径,例如
"C:\Users\<user>\Downloads\chromedriver_win32\chromedriver.exe"
。
I would suggest using the webdriver_manager
, as it will take care of downloading the latest stable driver based on the browser version and also takes care of the execution paths.我建议使用
webdriver_manager
,因为它将负责根据浏览器版本下载最新的稳定驱动程序,并负责执行路径。
Use the below 3 simple lines to start the chrome driver.使用以下 3 行简单的代码来启动 chrome 驱动程序。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager``
driver = webdriver.Chrome(ChromeDriverManager().install()
Make sure you have selenium
and webdriver_manager
libraries added to the project.确保您已将
selenium
和webdriver_manager
库添加到项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.