码迷,mamicode.com
首页 > Web开发 > 详细

PhantomJS使用Chrome或Firefox的无头版本来替代

时间:2019-12-12 19:57:17      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:cli   adl   condition   ble   options   实现   element   环境变量   了解   

CSDN上的孔天逸从Mozilla上提供了Selenium+Headless Firefox在Python上实现的方法:

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait
if __name__ == "__main__":
options = Options()
options.add_argument(‘-headless‘) # 无头参数
driver = Firefox(executable_path=‘geckodriver‘, firefox_options=options) # 配了环境变量第一个参数就可以省了,不然传绝对路径
wait = WebDriverWait(driver, timeout=10)
driver.get(‘http://www.google.com‘)
wait.until(expected.visibility_of_element_located((By.NAME, ‘q‘))).send_keys(‘headless firefox‘ + Keys.ENTER)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ‘#ires a‘))).click()
print(driver.page_source)
driver.quit()

作者也说了,本地要有Firefox;本地要有geckodriver,最好再配置一下环境变量;别每下载一个网页实例化一个webdriver(Firefox or Chrome)然后就close()掉,实例化webdriver的时间也是时间~推荐将下载器做成单例类或将webdirver做类变量。

对于Chrome,博客园上的zhuxiaoxi提供了解决方案(需要安装chromedriver):

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)
driver.get("https://cnblogs.com/")

PhantomJS使用Chrome或Firefox的无头版本来替代

标签:cli   adl   condition   ble   options   实现   element   环境变量   了解   

原文地址:https://www.cnblogs.com/gl3001/p/12031119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!