python - 运行使用远程 Webdriver 并连接到 selenium hub 和节点的测试失败

标签 python selenium selenium-grid remotewebdriver

概述:

我正在尝试在 ubuntu 服务器中使用 CHROME 浏览器运行一个使用 webdriver.Remote 的 python 脚本。我在服务器中设置了一个 selenium 网格(一个集线器和一个节点),并使用 systemd 服务文件来运行集线器和节点以及 xvfb 作为后台进程。但是,运行 python 文件给了我一个: selenium.common.exceptions.WebDriverException:

更多详细信息:

  • Selenium 服务器版本:3.4
  • Chromedriver 版本:2.29(已在/usr/local/bin 中 - 运行此版本没有问题)
  • Geckodriver 版本:0.15.0(我使用的是 Chrome 浏览器,但将其放在这里以防万一您需要它。
  • 我已经为 xvfb 导出 DISPLAY :5
  • 检查 netstat -tln 显示集线器和节点正在端口 4444 和 5555 上运行
  • 我正在通过 Fabric 自动设置 digital ocean 服务器。那么我该如何运行 python 脚本呢?我将代码放入 digital ocean 服务器中,并在那里运行代码
  • 如果您需要更多详细信息,请告诉我,但是当我检查节点的日志时,我根本没有看到任何表明节点中已创建 session 的消息

我一直在创建新的 Droplet,并花费了无数的时间来调试错误所在。

代码

对于 xvfb 进程:

[Unit]
Description=X Virtual Frame Buffer Service
After=network.target

[Service]
ExecStart=/usr/bin/Xvfb :5 -screen 0 1024x768x8

[Install]
WantedBy=multi-user.target

对于 Selenium 集线器:

[Unit]
Description=Selenium Hub
After=syslog.target network.target


[Service]
User=root
Type=simple
Environment=DISPLAY=:5
ExecStart=/usr/bin/java -Dwebdriver.gecko.driver=/usr/local/bin/geckodriver -Dwebdriver.chrome.bin=/usr/bin/google-chrome -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone.jar -role hub -log /var/log/selenium-hub/output.log


[Install] 
WantedBy=multi-user.target

这是我的 Selenium 节点的另一个

....

[Service]
User=root
Type=simple
Environment=DISPLAY=:5
ExecStart=/usr/bin/java -Dwebdriver.gecko.driver=/usr/local/bin/geckodriver -Dwebdriver.chrome.bin=/usr/bin/google-chrome -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register -log /var/log/selenium-node/output.log

这是我得到的错误

selenium.common.exceptions.WebDriverException: Message: None
Stacktrace:
     at java.util.HashMap.putMapEntries (HashMap.java:500)
     at java.util.HashMap.putAll (HashMap.java:784)
     at org.openqa.selenium.remote.DesiredCapabilities.<init> (DesiredCapabilities.java:55)
     at org.openqa.grid.web.servlet.handler.RequestHandler.process (RequestHandler.java:104)
....
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool.runJob (QueuedThreadPool.java:672
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool$2.run (QueuedThreadPool.java:590
at java.lang.Thread.run (Thread.java:745)

这是我如何使用remote.WebDriver

def create_driver():
    command_executor = 'http://localhost:4444/wd/hub'  # default
    capabilities = {
        'browserName': 'chrome',
        'version': '',
        'platform': 'ANY',
        'javascriptEnabled': 'True',
        # 'acceptSslCerts': 'False',
        # 'unexpectedAlertBehaviour': 'True',
        'chromeOptions': {
            'args': [
                '--disable-extensions',
                '--no-sandbox',
                '--disable-setuid-sandbox',
                '--verbose',
                '--log-path=/var/log/chromedriver-full.log',
                '--disable-impl-side-painting',
                '--memory-model=high',
                '--disk-cache-size=500000000',
                '--allow-running-insecure-content',
                '--ignore-certificate-errors',
                '--ignore-urlfetcher-cert-requests',
                '--disable-gpu',
                '--disk-cache-dir=null',
            ],
            # 'extensions': [],
            # 'binary': '/usr/bin/chromium-browser',
            # 'minidumpPath': '/root/'
        },
        'loggingPrefs': {'driver': 'ALL', 'server': 'ALL', 'browser': 'ALL', 'performance': 'ALL'}
    }
    driver = webdriver.Remote(desired_capabilities=capabilities, command_executor=command_executor)
    time.sleep(5)  # Let the user actually see something!
    driver.implicitly_wait(60)
    return driver


def main():
    print("Running remote web driver test")
    driver = create_driver()
    driver.get("https://www.google.com.ph")
    print(driver.current_url)
    screenshot = driver.save_screenshot('/root/test.png')
    print(screenshot)
    driver.quit()
    display.stop()
    print("Success test on remote web driver")

main()

最佳答案

我在测试 selenium-extras 时遇到了同样的错误...

我认为 python-selenium 的最新版本(3.4.1)在与 Selenium GRID(集线器+节点)交互时存在错误,而在独立模式下运行良好。

我使用旧版 python selenium 解决了这个问题:

pip show selenium | grep -i version
Version: 2.48.0

如果你是用pip安装的,可以先卸载它:

pip uninstall selenium

然后尝试不同的版本,我使用 APT 提供的相同版本进行了成功尝试:

pip install selenium==2.48.0

也许某些更高版本可以工作,但没有尝试。

再见!

关于python - 运行使用远程 Webdriver 并连接到 selenium hub 和节点的测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769237/

相关文章:

python - 数据的指数拟合(python)

javascript - 在 python 和 node.js 中复制 java.lang.String.hashCode() 输出的函数

javascript - 失败 : unknown error: Element is not clickable at point (x, x) - Angular/Protractor

teamcity - Selenium 网格和 TeamCity 集成

Python:如何从另一个进程非阻塞地读取标准输出?

python - 将 matplotlib 的鼠标按钮事件与 pick 事件结合起来

java - 自定义 WebDriver 无法与 Selenium Actions 方法交互

java - 使用 Selenium Webdriver 实现 headless /无 GUI 自动化

selenium - Selenium WebDriver 可以在后台静默打开浏览器窗口吗?

selenium-webdriver - Saucelabs: org.openqa.selenium.WebDriverException: The test with session id *** has already finished, and can't receive further commands 的问题