有人可以帮助运行它一个Python和MacOS(catalina?(
我尝试在Windows环境中使用它,按预期工作,但不幸的是,无法在Mac上执行此操作。 似乎需要一些配置
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
class Website:
def __init__(self):
self.driver = webdriver.Edge(executable_path='/Users/dannyk/Downloads/msedgedriver')
def reach_web_docker(self):
self.driver.maximize_window()
self.driver.get('http://127.0.0.1:9080/')
if __name__ == '__main__':
reg1 = Website()
reg1.reach_web_docker()
我收到以下错误,不幸的是,这并没有真正解释问题所在
>> runfile('/Users/dannyk/PycharmProjects/new_prj/t1.py', wdir='/Users/dannyk/PycharmProjects/new_prj')
/Users/dannyk/PycharmProjects/new_prj/t1.py:16: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
self.driver = webdriver.Edge(executable_path='/Users/dannyk/Downloads/msedgedriver')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "/Users/dannyk/PycharmProjects/new_prj/t1.py", line 57, in <module>
reg1 = Website()
File "/Users/dannyk/PycharmProjects/new_prj/t1.py", line 16, in __init__
self.driver = webdriver.Edge(executable_path='/Users/dannyk/Downloads/msedgedriver')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/edge/webdriver.py", line 65, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.EDGE['browserName'], "ms",
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 79, in __init__
RemoteWebDriver.__init__(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 183, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 280, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 349, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 240, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Stacktrace:
0 msedgedriver 0x000000010e547ef9 msedgedriver + 12562169
1 msedgedriver 0x000000010e486a23 msedgedriver + 11770403
2 msedgedriver 0x000000010da59d2d msedgedriver + 1101101
3 msedgedriver 0x000000010d99465e msedgedriver + 292446
4 msedgedriver 0x000000010d993e82 msedgedriver + 290434
5 msedgedriver 0x000000010d9954fd msedgedriver + 296189
6 msedgedriver 0x000000010d991cb3 msedgedriver + 281779
7 msedgedriver 0x000000010d96ac2d msedgedriver + 121901
8 msedgedriver 0x000000010d96bc25 msedgedriver + 125989
9 msedgedriver 0x000000010e4e109f msedgedriver + 12140703
10 msedgedriver 0x000000010e4f39da msedgedriver + 12216794
11 msedgedriver 0x000000010e4f3769 msedgedriver + 12216169
12 msedgedriver 0x000000010e49e539 msedgedriver + 11867449
13 msedgedriver 0x000000010e4f3f63 msedgedriver + 12218211
14 msedgedriver 0x000000010e4ca783 msedgedriver + 12048259
15 msedgedriver 0x000000010e510c24 msedgedriver + 12336164
16 msedgedriver 0x000000010e551697 msedgedriver + 12600983
17 libsystem_pthread.dylib 0x00007fff69056109 _pthread_start + 148
18 libsystem_pthread.dylib 0x00007fff69051b8b thread_start + 15
所以,回答它,感谢 @Deepak-MSFT ,这奏效了。
只需要两件事:
发送驱动程序路径(就我而言,我需要在本地移动它,并发送 ABS 路径
发送功能。另外,我删除了所有功能,并放置了一个空字典。
这是代码。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
desired_cap = {
}
class Website:
def __init__(self):
self.driver = webdriver.Edge(executable_path='/Users/dannyk/PycharmProjects/new_prj/msedgedriver',capabilities=desired_cap)
def reach_web_docker(self):
self.driver.maximize_window()
self.driver.get('http://127.0.0.1:9080/')
if __name__ == '__main__':
reg1 = Website()
reg1.reach_web_docker()
我尝试检查错误消息并发现此异常。
selenium.common.exceptions.SessionNotCreatedException: 消息: 未创建会话:未找到匹配的功能
看起来您需要传递在 MAC OS 上运行此代码的功能。
下面是可以尝试传递的功能示例。
desired_cap = {
"os" : "OS X",
"os_version" : "Catalina",
"browser" : "Edge",
"browser_version" : "83.0",
"browserstack.local" : "false",
"browserstack.selenium_version" : "3.5.2"
}
您可以根据环境修改这些值。
参考:
selenium.webdriver.common.desired_capabilities
您需要将所需的功能作为参数在此行中传递。
self.driver.get('http://127.0.0.1:9080/')
您可以进行测试,并让我们知道您的测试结果。