美文网首页
selenium远程执行

selenium远程执行

作者: 假程序员 | 来源:发表于2020-06-01 23:47 被阅读0次

请确保远程服务器已运行对应的浏览器驱动程序,且正确设置运行参数。
以chromedriver为例:本例中服务器的ip是192.168.31.254
chromedriver --h 可以显示帮助信息
chromedriver --port=8000 --whitelisted-ips 可以启动一个被远程连接的、端口是8000的服务

第一种情况:服务器中chrome浏览器在Path中

import selenium.webdriver.common.desired_capabilities

if __name__ == '__main__':
    desired_capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities().CHROME.copy()

    driver = selenium.webdriver.Remote(command_executor="http://192.168.31.254:8000",
                                       desired_capabilities=desired_capabilities)
    driver.get("https://www.baidu.com/")
    driver.close()
    driver.quit()

第二种情况:服务器中chrome浏览器不在Path中

import selenium.webdriver.common.desired_capabilities

if __name__ == '__main__':
    chrome_options = dict()
    chrome_options["binary"] = r"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"  # where chrome is
    desired_capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities().CHROME.copy()
    desired_capabilities["goog:chromeOptions"] = chrome_options

    driver = selenium.webdriver.Remote(command_executor="http://192.168.31.254:8000",
                                       desired_capabilities=desired_capabilities)
    driver.get("https://www.baidu.com/")
    driver.close()
    driver.quit()

相关文章

网友评论

      本文标题:selenium远程执行

      本文链接:https://www.haomeiwen.com/subject/bhbkohtx.html