解决方法

查看导包是否正确:

  • 如果是webdriver.Chrome,那么导包的一切都要是chrome的
  • 如果是webdriver.Firefox,那么导入的包也都源于firefox的包

出现这个问题是因为由于selenium很多类都很相似,如selenium.webdriver.firefox.options与selenium.webdriver.chrome.options,导致导包的时候容易出错,而代码本身是没有问题的。

问题解析

错误代码如下:

from selenium.webdriver.firefox.options import Options
from selenium import webdriver

url = 'https://www.baidu.com'
options = Options()
options.add_argument('--headless')
browser = webdriver.Chrome(options=options)
browser.get(url=url)
print(browser.page_source[:300])
browser.close()

错误出现在第一行,应该改为:from selenium.webdriver.chrome.options import Options

更多推荐