使用Python中的远程WebDriver设置Chrome DevTools(Selenium 4)



我一直在尝试使用Selenium 4和Python来设置Chrome DevTools的使用。我已经能够让它在本地运行(没有任何异步的东西(,但当我尝试使用网络驱动程序时。远程实现,它崩溃了。

以下是Selenium文档中的一个示例:https://www.selenium.dev/de/documentation/support_packages/chrome_devtools/

以下是我尝试运行它的方式。

import asyncio
from selenium import webdriver
import selenium.webdriver.common.devtools.v96 as devtools
async def geo_location_test():
try:
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Remote(
command_executor='http://D5365900:4444/wd/hub',
options=chrome_options
)
async with driver.bidi_connection() as session:
cdp_session = session.session
await cdp_session.execute(devtools.emulation.set_geolocation_override(latitude=41.8781,
            longitude=-87.6298,
            accuracy=100))
driver.get("https://my-location.org/")
finally:
driver.quit()

async def main():
await geo_location_test()
if __name__ == "__main__":
asyncio.run(main())

它一直运行到行async with driver.bidi_connection() as session:(会话已建立,Chrome浏览器已打开(。但它与以下轨迹一起崩溃。

Traceback (most recent call last):
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 54, in <module>
asyncio.run(main())
File "C:Program FilesPython310libasynciorunners.py", line 44, in run
return loop.run_until_complete(main)
File "C:Program FilesPython310libasynciobase_events.py", line 641, in run_until_complete
return future.result()
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 51, in main
await geo_location_test()
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 40, in geo_location_test
async with driver.bidi_connection() as session:
File "C:Program FilesPython310libcontextlib.py", line 199, in __aenter__
return await anext(self.gen)
File "C:Program FilesPython310libsite-packagesseleniumwebdriverremotewebdriver.py", line 1576, in bidi_connection
async with cdp.open_cdp(ws_url) as conn:
File "C:Program FilesPython310libcontextlib.py", line 199, in __aenter__
return await anext(self.gen)
File "C:Program FilesPython310libsite-packagesseleniumwebdrivercommonbidicdp.py", line 457, in open_cdp
async with trio.open_nursery() as nursery:
File "C:Program FilesPython310libsite-packagestrio_core_run.py", line 796, in __aenter__
self._scope.__enter__()
File "C:Program FilesPython310libsite-packagestrio_core_ki.py", line 159, in wrapper
return fn(*args, **kwargs)
File "C:Program FilesPython310libsite-packagestrio_core_run.py", line 449, in __enter__
task = _core.current_task()
File "C:Program FilesPython310libsite-packagestrio_core_run.py", line 2285, in current_task
raise RuntimeError("must be called from async context") from None
RuntimeError: must be called from async context

正如您所看到的,我使用的是Python 3.10。我还将Selenium绑定升级到4.1.0,并运行Selenium 4.0.0集线器/节点配置来自动化Chrome 96。

有什么想法吗,这里的问题是什么?我是否错误地处理了异步协同程序?

非常感谢您的帮助!

更新在尝试用trio运行它之后(正如Henry Ashton Martyn的评论中所建议的(,我得到了以下错误。

Traceback (most recent call last):
File "C:Program FilesPython310libsite-packagestrio_highlevel_open_tcp_stream.py", line 332, in attempt_connect
await sock.connect(sockaddr)
File "C:Program FilesPython310libsite-packagestrio_socket.py", line 682, in connect
raise OSError(err, "Error in connect: " + os.strerror(err))
OSError: [Errno 10049] Error in connect: Unknown error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 58, in <module>
trio.run(main)
File "C:Program FilesPython310libsite-packagestrio_core_run.py", line 1932, in run
raise runner.main_task_outcome.error
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 55, in main
await geo_location_test()
File "C:Usersy04082eclipse-workspaceWWI-TestautomationTestScriptsJoshasync_sel_4.py", line 44, in geo_location_test
async with driver.bidi_connection() as session:
File "C:Program FilesPython310libcontextlib.py", line 199, in __aenter__
return await anext(self.gen)
File "C:Program FilesPython310libsite-packagesseleniumwebdriverremotewebdriver.py", line 1576, in bidi_connection
async with cdp.open_cdp(ws_url) as conn:
File "C:Program FilesPython310libcontextlib.py", line 199, in __aenter__
return await anext(self.gen)
File "C:Program FilesPython310libsite-packagesseleniumwebdrivercommonbidicdp.py", line 458, in open_cdp
conn = await connect_cdp(nursery, url)
File "C:Program FilesPython310libsite-packagesseleniumwebdrivercommonbidicdp.py", line 479, in connect_cdp
ws = await connect_websocket_url(nursery, url,
File "C:Program FilesPython310libsite-packagestrio_websocket_impl.py", line 262, in connect_websocket_url
return await connect_websocket(nursery, host, port, resource,
File "C:Program FilesPython310libsite-packagestrio_websocket_impl.py", line 171, in connect_websocket
stream = await trio.open_tcp_stream(host, port)
File "C:Program FilesPython310libsite-packagestrio_highlevel_open_tcp_stream.py", line 367, in open_tcp_stream
raise OSError(msg) from trio.MultiError(oserrors)
OSError: all attempts to connect to 0.0.0.0:4444 failed

selenium似乎没有使用python的内置异步框架,而是使用了一个名为trio的包。您应该能够通过更改以下代码来解决此问题:

if __name__ == "__main__":
asyncio.run(main())

至:

if __name__ == "__main__":
import trio
trio.run(main)

相关内容

  • 没有找到相关文章

最新更新