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