硒未连接到Safari Web驱动程序

问题描述

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

我正在使用MacBook(10.15.6)和Safari(13.1.2)。我正在使用PyCharm 2020.1.4,并使用硒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

解决方法

要使用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。