将Selenium与Pyce一起使用时的弃用问题

2024-09-30 01:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在MacOS上通过PyCharm CE运行Selenium,并尝试使用Google Chrome运行它

但是,每当我运行以下命令时:

from selenium import webdriver

browser = webdriver.Chrome("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")

browser.get('https://inventwithpython.com')

结果是:

/Users/louiscage/PycharmProjects/SeleniumPractice/SeleniumPractice.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Chrome("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")

我已经下载了Mac64的Chromedriver和Geckodriver,并将其放置在PyCharm项目的适当目录中。但我似乎仍然找不到解决这个问题的办法。任何帮助都将不胜感激


Tags: from命令browserseleniumgooglemacoschromechromedriver
2条回答

这个弃用问题出现在Selenium、Pip和Python更新中。为此,您只需执行以下操作:

使用以下代码

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
s = Service(ChromedriverManager().install())
browser = webdriver.Chrome(service=s)
browser.get('https://inventwithpython.com')

正如警告所说:please pass in a Service object,因此您应该这样做:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service

    service = Service("/Users/louiscage/PycharmProjects/SeleniumPractice/chromedriver")
    browser = webdriver.Chrome(service=service)

    browser.get('https://inventwithpython.com')

在chromeService类上传递可执行路径,并将其放在可变服务上。然后将其传递给webdriver chrome

此处的链接引用:Selenium Chrome webdriver service

相关问题 更多 >

    热门问题