selenium远程执行

请确保远程服务器已运行对应的浏览器驱动程序,且正确设置运行参数。
以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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。