【问题标题】:Selenium not connecting to Safari Web DriverSelenium 未连接到 Safari Web 驱动程序
【发布时间】:2020-09-16 19:49:17
【问题描述】:

首先让我说,我之前已经多次看到这个问题,并且我已经阅读了大多数解决方案,但我仍然遇到问题。

我使用的是 MacBook (10.15.6) 和 Safari (13.1.2)。我正在使用 PyCharm 2020.1.4 并使用 selenium 3.14.1 为 python 3.8 编写了一个程序,该程序连接到 Youtube(实际上是任何网站——我也尝试过 Google)并发送搜索词。我一直遇到的问题是 Selenium 无法连接到 Web 驱动程序。

我已按照之前帖子中的步骤允许从开发人员选项卡进行远程自动化,以及从终端屏幕手动执行 safari 驱动程序 (safaridriver --enable)。

我也为 Chrome 尝试了类似的示例连接代码,并且得到了类似的错误。

这是我正在运行的代码:

from selenium import webdriver

driver = webdriver.Safari()
driver.get('https://youtube.com')
searchbox = driver.find_element_by_xpath('//*[@id="search"]')
searchbox.send_keys('Paint Drying')   

这是 Tracebacklog::

/Users/Fudgey/PycharmProjects/Web_Scrape/venv/bin/python /Users/Fudgey/PycharmProjects/Web_Scrape/web_auto_seln_v0.py
Traceback (most recent call last):
  File "/Users/Fudgey/PycharmProjects/Web_Scrape/web_auto_seln_v0.py", line 3, in <module>
    driver = webdriver.Safari()
  File "/Users/Fudgey/PycharmProjects/Web_Scrape/venv/lib/python3.8/site-packages/selenium/webdriver/safari/webdriver.py", line 56, in __init__
    self.service.start()
  File "/Users/Fudgey/PycharmProjects/Web_Scrape/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/bin/safaridriver

【问题讨论】:

  • 这个链接详细说明了我已经采取的步骤,但我仍然遇到同样的问题。
  • 不确定是否有帮助,但我尝试了一个简单的 Chrome 驱动程序分配,我得到了同样的错误。这是代码'from selenium import webdriver driver = webdriver.Chrome()'

标签: python macos selenium selenium-webdriver safaridriver


【解决方案1】:

为了使用 Safari,您需要检查浏览器的允许自动化远程选项,按照以下步骤操作:

1. Open Safari
2. Go to Safari > Preferences
3. Go to Advanced and in the bottom, there is an option "Show Develop menu in menu bar"
4. Check that option
5. Go to Develop
6. Check on "Allow Remote Automation"
7. Close/Quit the browser

现在您可以通过代码安全地运行 Safari。

【讨论】:

  • 我试过了,还是连接不上WebDriver。我尝试通过 PyCharm 和 IDLE 运行代码,最后通过 python 控制台本身运行。都给出相同的错误。
猜你喜欢
  • 2016-06-30
  • 2016-08-11
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多