gpt4 book ai didi

Selenium 远程 Webdriver 无法与 AWS EC2 一起使用

转载 作者:行者123 更新时间:2023-12-04 19:01:16 29 4
gpt4 key购买 nike

我正在尝试做一些网站测试,需要保持旧的 webdriver 打开,然后使用 webdriver.remote 使用旧驱动程序的执行程序 url 和 session id 重新附加。相同的代码在我的 MacBook 上运行良好,但在 AWS EC2 Ubuntu 16.04 上运行出错。错误回溯和代码附在下面。请帮忙。

操作系统:Ubuntu 16.04

Selenium 版本:3.4.0

浏览器:谷歌-Chrome

enter image description here enter image description here

New Code following @Tarun Lalwani's Idea

最佳答案

在为这个问题挖掘了这么久之后,我终于自己找到了解决方案。事实证明,没有 GUI 的 Ubuntu 很难处理。因此,当您尝试启动 Selenium Webdriver 时。您需要添加一些选项,不仅是常规的 webdriver.Chrome,还需要 webdriver.Remote。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import
DesiredCapabilities

options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('headless')
options.add_argument('--no-sandbox')

driver = webdriver.Chrome(chrome_options=options)
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://www.google.com")

print(session_id)
print(executor_url)

print(driver.current_url)

driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities=options.to_capabilities())
driver2.close()
driver2.session_id = session_id
print(driver2.current_url)
driver2.get("http://www.facebook.com")
print(driver2.current_url)

此外,拥有 webdriver.remote 也会打开一个僵尸网络驱动程序。如果您只关心重新附加到现有的 webdriver。您可以在将新驱动程序连接回旧驱动程序之前关闭新驱动程序。

关于Selenium 远程 Webdriver 无法与 AWS EC2 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47060735/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com