python3.6 selenium webdriver error X display是发送密钥无法使用Xvfb所必需的

2024-04-28 17:03:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用硒网络驱动程序.Chrome为我的测试环境创建具有特定功能的webdriver。在

    class MyDriver(webdriver.Chrome):
          def __init__(self, executable_path="chromedriver", port=0,
              chrome_options=None, service_args=None,
              desired_capabilities=None, service_log_path=None):
          super().__init__(executable_path, port, chrome_options, service_args,
                      desired_capabilities, service_log_path)  
              # vdisplay = Xvfb()
              # vdisplay.start()

测试的一部分包括使用element.send_keys(value)发送密钥, 测试运行良好,直到几天前它开始崩溃,并显示以下消息:

"selenium.common.exceptions.WebDriverException: Message: unknown error: an X display is required for keycode conversions, consider using Xvfb (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.10.0-22-generic x86_64)"

我一遍又一遍地搜索和研究我不明白这个错误。。。在

我用的是:

  • Python 3.6
  • 皮查姆
  • chromedriver linux_64bit 2.27.440175

我找到的唯一有用的链接是chromium在:https://bugs.chromium.org/p/chromedriver/issues/detail?id=1772打开的一个bug,但是我不熟悉这些工具。。。在

我想知道,如果有人有一个解决方案,发送钥匙或修复这个错误,我已尝试删除铬和降级它。。。没用!在

任何帮助都将不胜感激!在

谢谢


Tags: pathnoneloginitportserviceargschrome
1条回答
网友
1楼 · 发布于 2024-04-28 17:03:23

在使用Xvfb很长一段时间后,我采用了一种新的方法来运行不显示selenium测试:

class MySolutionsDriver(webdriver.Chrome):
    def __init__(self, executable_path=PATH_chromedriver, port=0,
             chrome_options=None, service_args=None,
             desired_capabilities=None, service_log_path=None):
        # vdisplay = Xvfb()
        # vdisplay.start()

        # for full screen uncheck two lines below and import of Options
        chrome_options = Options()
        chrome_options.add_argument(' headless')
        chrome_options.add_argument(' start-maximized')
        chrome_options.add_argument('disable-infobars')
        chrome_options.add_argument(' disable-extensions')
        chrome_options.add_argument(' no-sandbox')
        chrome_options.add_argument(' disable-dev-shm-usage')
        chrome_options.add_argument(" incognito")

        super().__init__(executable_path, port, chrome_options, service_args,
                     desired_capabilities, service_log_path)

使用chrome选项并使用add_argument函数添加参数,为了在不显示的情况下运行测试,我使用>;“headless”。在

除此之外,我发现了很多可以补充的好论点!在

希望这能帮助任何人解决这个问题。在

PS:仅供参考-Xvfb不适用于windows(或据我所知的Microsoft服务器)。在

相关问题 更多 >