关于macos:用python设置selenium,用于mac中的safari浏览器

selenium setup with python for safari browser in mac

AM在Mac中使用Selenium和Python,并且在Safari浏览器中运行测试用例时面临问题。

命令sudo easy_install selenium在终端中打印以下内容:

Searching for selenium
Best match: selenium 3.5.0
Processing selenium-3.5.0-py2.7.egg
selenium 3.5.0 is already the active version in easy-install.pth

Using /Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg
Processing dependencies for selenium
Finished processing dependencies for selenium

现在,当我想运行一个简单的python脚本来打开一个网站时,我使用以下代码:

1
2
3
4
5
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Safari()
driver.get("https://www.google.co.in")

它在终端中显示此错误:

Traceback (most recent call last):
File"a.py", line 5, in
driver = webdriver.Safari()
File"/Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg/selenium/webdriver/safari/webdriver.py", line 52, in init
desired_capabilities=desired_capabilities)
File"/Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 140, in init
self.start_session(desired_capabilities, browser_profile)
File"/Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File"/Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File"/Library/Python/2.7/site-packages/selenium-3.5.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Could not create a session: You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver

我找不到确切的问题。

请帮忙并提前感谢。


打开Safari并在"开发"菜单中选中EDOCX1的选项〔0〕。

Enable Remote Automation

在终端上运行一次safaridriver -p 5555,如果需要许可,就给它。这只需要一次。有关详细信息,请参阅以下文章

WebDriver Support in Safari 10