之前的一篇文章使用selenium模拟登录解决滑块验证问题以及wy滑块验证登录,使用的是Chrome,但它本身不是无头浏览器,这样在Linux环境下就无法正常调用,所以考虑使用PhantomJS进行模拟登录。看了半天,发现依然可以使用Selenium,在创建webdriver的时候只要调用PhantomJS就可以了,像这样:

driver = webdriver.PhantomJS(executable_path="phantomjs.exe")

但是运行的时候又报错了:

Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead

怎么办?经查找资料,发现调用Chrome的时候竟然可以使用无头的,使用方法如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
然后就可以“不用浏览”的方式使用Selenium调用Chrome进行模拟登录了。
Logo

更多推荐