在Linux系统下安装完selenium后,测试一下是否安装正确,运行如下测试代码,发现显示ImportError: cannot import name 'webdriver'错误提示,开始以为安装路径写错了,其实我安装的位置在程序中已经不需要指定driver_path,经查找资料后发现了原因,我为了方便直接将文件命名为selenium.py ,在运行程序时Python 会首先导入当前我的selenium.py文件,而不是导入标准库中的selenium.py文件,所以就不能导入webdriver。
知道了原因这个问题就很好解决了,我们可以直接将自己的文件重命名就可以了。
from selenium import webdriver
driver_path = r'/usr/bin/chromedriver'
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('http://baidu.com')
print(driver.page_source)