linux系统下使用selenium驱动Chrome

写在前面:

开始使用的是xvfb + PyVirtualDisplay + firefox(60.0,centos7默认yum源的版本,ubuntu16.04安装了65.0也也试过),遇到一个比较大的坑,再模拟登录dowjones.com这个网站后,截图正常,做了一些操作,然后再截图,发现截的图只有很小一块,如下图所示

不管是调整窗口最大化还是设置尺寸,都不能解决问题,最终只能放弃,改换google-chrome-stable

换Chrome:

安装

参看Ubuntu 16.04下安装64位谷歌Chrome浏览器

踩到的坑一:

中文乱码,解决方法:

centos:

yum groupinstall fonts

 ubuntu:

sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

踩到的坑二:

不能截图,抛time out异常

selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000

解决方法:

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--no-sandbox")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-browser-side-navigation")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1024, 768)
driver.get_screenshot_as_file(STATIC_FOLDER + home_img_url)
driver.close()

猜你喜欢

转载自blog.csdn.net/fuhtead/article/details/87884036