瞎扯一句

最近在做一个关于 selenium 相关的项目,在选择浏览器方面,一般有3种方案:

  1. chrome
  2. phantomJs
  3. firefox(推荐)

网上有很多教程是关于PhantomJS的,可是,在2018.3.4日,git开源项目上,ariya宣布暂停更新,具体时间另行通知,截止到2019.3.8日,还没消息。。。

chrome浏览器的教程也是很多的,但是,经过这几天的使用,体验并不是很好,对selenium超时的支持不够好,坑了我很久!

在这里隆重推荐firefox浏览器

简介

利用selenium的强大之处在于,可以像人打开浏览器一样,这样可以避免js的各种加密,动态加载之类的,可见即可爬。但是,selenium控制的chrome会暴露出许多参数,是可以通过这些参数来识别selenium的,现在针对selenium的反爬网站其实很多了。听说可以使用pyppeteer(puppeteer的py版),以后要学。

今天发现一个好用的方法driver.set_page_load_timeout(self.timeout), 让我不得不仔细学习几种超时的用法了

  1. driver.implicitly_wait(1)
  2. driver.set_page_load_timeout(15)
  3. driver.set_script_timeout(15)

1.implicitly_wait

官方注释:

Sets a sticky timeout to implicitly wait for an element to be found,

or a command to complete. This method only needs to be called one

time per session. To set the timeout for calls to

execute_async_script, see set_script_timeout.

当你在调用find_element_by...方法时,会用到此方法,并且它是driver全局的,只要设置1次,所以当你想查找某元素,找不到马上放弃时,要设置得比较小才行

2.set_page_load_timeout

官方注释:

Set the amount of time to wait for a page load to complete

before throwing an error.

当你在调用driver.get()方法打开某网站时,其实某网站已经差不多加载完成了,但比如某图片,某异步请求没完成,一直转圈圈,get方法是不会结束的,set_page_load_timeout就是来设置这个的,示例:

driver = webdriver.Chrome()
driver.set_page_load_timeout(15) # 设定页面加载限制时间 try:
driver.get('https://www.toutiao.com')
except TimeoutException:
driver.execute_script('window.stop()') # 停止加载 print(driver.page_resource)
driver.quit()

但是!!!chrome浏览器不支持这个非常重要的配置,一旦TimeoutException,driver的所有操作都会报TimeoutException异常,不能进行下去了。所以我推荐firefox浏览器

3.set_script_timeout

官方注释:

Set the amount of time that the script should wait during an

execute_async_script call before throwing an error.

这是控制异步脚本执行时间,超时抛TimeoutException异常

4.WebDriverWait Class

示例:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 15, 0.5)
wait.until(EC.presence_of_element_located((By.XPATH, 'express')))

我喜欢使用的找元素的等待类,15秒超时,每0.5秒根据要求找元素,找到了就结束until,15后没找到会抛TimeoutException异常

最后放模板

1.set_page_load_timeout模板

from selenium.common.exceptions import TimeoutException

t0 = time.time()
print("cur time is: %s" % (time.time() - t0))
driver = webdriver.Chrome()
driver.set_page_load_timeout(5) # 设定页面加载限制时间
driver.maximize_window() try:
print("cur time is: %s" % (time.time() - t0))
driver.get('http://www.autohome.com.cn/')
except TimeoutException:
print("cur time is: %s" % (time.time() - t0))
try:
driver.execute_script('window.stop()') # 当页面加载时间超过设定时间,通过执行Javascript来stop加载,即可执行后续动作
except:
pass
print("cur time is: %s" % (time.time() - t0))

2.selenium + chrome 模板

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('disable-infobars') # 去掉提示
# 一定要注意,=两边不能有空格,不能是这样"--proxy-server = http://202.20.16.82:10152"
# chrome_options.add_argument("--proxy-server=http://192.168.60.15:808") # 设置代理 # chrome_options.add_argument('start-fullscreen') # 启动就全屏 F11那种 # chrome_options.add_argument('-lang=zh-CN') # 中文,貌似没用
# 语言,设为中文
# prefs = {'intl.accept_languages': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'}
# chrome_options.add_experimental_option('prefs', prefs) # chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
# chrome_options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 # chrome_options.add_argument('window-size=1920x3000') # 指定浏览器分辨率
# chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
# chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 # chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使用的浏览器位置 TIMEOUT = 15 class Display(object): def __init__(self):
self.driver = webdriver.Chrome(options=chrome_options) # 配置好了环境变量可以不用写executable_path
# self.driver.set_page_load_timeout(TIMEOUT)
self.wait = WebDriverWait(self.driver, TIMEOUT, 0.5) def __del__(self):
if self.driver:
self.driver.close() def fetch(self, url):
self.driver.maximize_window() # 放大 self.driver.get(url) # 发请求 # self.driver.execute_script('window.location.reload();') # 刷新 self.wait.until(EC.presence_of_element_located((By.ID, 'kw')))
self.driver.find_element_by_id('kw').send_keys('selenium') self.driver.find_element_by_id('su').click() self.wait.until(EC.presence_of_element_located((By.ID, '1'))) return self.driver.page_source d = Display()
print(d.fetch('https://www.baidu.com'))

3selenium + firefox 模板

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC TIMEOUT = 15 class Display(object): def __init__(self):
# 无界模式
options = webdriver.FirefoxOptions()
options.headless = True
profile = webdriver.FirefoxProfile()
# 禁用图片
profile.set_preference('permissions.default.image', 2) self.driver = webdriver.Firefox(desired_capabilities=DESIRED_CAP, profile=profile,
options=options)
self.driver.set_page_load_timeout(TIMEOUT)
self.wait = WebDriverWait(self.driver, TIMEOUT, 0.5) def __del__(self):
if self.driver:
self.driver.close() def fetch(self, url):
self.driver.maximize_window() # 放大 self.driver.get(url) # 发请求 # self.driver.execute_script('window.location.reload();') # 刷新 self.wait.until(EC.presence_of_element_located((By.ID, 'kw')))
self.driver.find_element_by_id('kw').send_keys('selenium') self.driver.find_element_by_id('su').click() self.wait.until(EC.presence_of_element_located((By.ID, '1'))) return self.driver.page_source

python3 + selenium + (chrome and firefox)使用的更多相关文章

  1. Python3 + selenium + Chrome浏览器(webdriver.Chrome()报错)

    Python3 + selenium + Chrome浏览器 Error: selenium.common.exceptions.WebDriverException: Message: 'chrom ...

  2. Centos7安 装python3+Selenium+chrome+chromedriver

    Centos7安装python3+Selenium+chrome+chromedriver详细python2和python3共存,Selenium错误的处理更新Centos源 wget -O /etc ...

  3. Selenium+Chrome或Firefox的动态爬虫程序

    新版本的Selenium不再支持PhantomJS了,请使用Chrome或Firefox的无头版本来替代.

  4. centos7+python3+selenium+chrome

    一.安装GUI图形化界面 (1)安装GUI图形化界面 yum groupinstall "GNOME Desktop" "Graphical Administration ...

  5. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  6. python selenium环境配置Firefox和Chrome

    1.下载Selenium库,可以使用pip install selenium https://pypi.python.org/pypi/selenium/ 2.下载驱动 Chrome: https:/ ...

  7. Selenium 启动无头浏览器,只有chrome 和 firefox的,没有IE

    使用无头浏览器,可以避免掉不确定的弹出框对脚本稳定性的影响,还能在脚本执行过程中省略掉了css 加载的时间. 以下是Selenium 启动无头浏览器的方法,只有chrome 和 firefox的. p ...

  8. python3 selenium模块Chrome设置代理ip的实现

    python3 selenium模块Chrome设置代理ip的实现 selenium模块Chrome设置代理ip的实现代码: from selenium import webdriver chrome ...

  9. linux 下 命令行中运行 selenium chrome 问题

    1.chrome 现在不允许使用root运行了. 2.无界面 chromedriver 调用chrome 会出错. <另外一定要匹配 chromedriver和chrome 的版本. 要不会出各 ...

随机推荐

  1. 利用国内的源安装 Python第三方库

    我们需要安装一些Python的第三方库,但是使用  pip install + 第三方库  的时候,会出现下载速度慢的问题,当然我们也可以使用国内的源安装. 例如: sudo pip install ...

  2. JStorm环境搭建

    开始JStorm学习之前需要搭建集群环境,这里演示搭建单机JStorm环境,仅供学习使用,生产环境部署大同小异,但建议参考JStorm社区及相关说明文档. 一.前提 JStorm核心代码均用Java实 ...

  3. Confluence - Online Team Collaboration Tool

    Confluence - 在线的团队协作沟通工具,包含 meeting 管理, wiki 等等功能 https://www.atlassian.com/software/confluence

  4. 微信client内部推荐项目总结

    如今实习的公司在面向企业提供招聘服务领域数一数二,而下半年的产品重点就在于移动端微信招聘项目.而这次内推项目开发属于微信招聘一个分支.     一.内推综述     乐帝之前读<招聘与录用> ...

  5. atitit. 研发管理---如何根据自己的特挑选 产业、行业、职业、岗位与自己发展的关系

    atitit. 研发管理---如何根据自己的特挑选 产业.行业.职业.岗位与自己发展的关系 1. 产业及分类 1 2. 二.行业 2 3. 职业概念- 3 4. 职业划分 3 5. 职业兴趣分类 4 ...

  6. atitit.XML类库选型及object 对象bean 跟json转换方案

    atitit.XML类库选型及object 对象bean 跟json转换方案 1. XML类库可以分成2大类.标准的.这些类库通常接口和实现都是分开的 1 2. 常见的xml方面的方法 2 2.1.  ...

  7. python操作excel之 模块 xlrd (详解)

    二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') 3.使用技巧 获取一个工作表 ...

  8. hadoop onekey_step2

    #onekey_step2 # Rrogram: # 安装hadoop简易集群程序 # 使用说明 # History: # -- luoqi v0. release # email: # @qq.co ...

  9. oozie中事件触发input-events和done-flag

    样例如下: <coordinator-app name="test_job" frequency="${coord:days(1)}" start=&qu ...

  10. 个别图片IE中无法显示问题

    今天有人保障,某些图片在IE下无法打开,但是其他浏览器均没有问题.以前还真没遇到过这类问题,从上至下查看了一遍,能排除的因素基本都排除了,还是不知道为什么不能显示,真是奇怪了.最后注意到无法显示的图片 ...