Python selenium + Firefox启动浏览器
驱动下载:http://chromedriver.storage.googleapis.com/index.html
https://github.com/mozilla/geckodriver/releases
一、selenium启动Firefox浏览器。
1 from selenium import webdriver
# from selenium.webdriver.firefox.firefox_profile import FirefoxProfile user_agent = 'Mozilla/5.0 (Linux; Android 7.0; BND-AL10 Build/HONORBND-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044304 Mobile Safari/537.36 MicroMessenger/6.7.3.1340(0x26070331) NetType/4G Language/zh_CN Process/tools' proxy = '127.0.0.1:5000'
proxy = proxy.split(':') # selenium headless 启动无头模式
options = webdriver.FirefoxOptions()
options.add_argument('-headless') # 第一步:创建一个FirefoxProfile实例
profile = webdriver.FirefoxProfile()
# 第二步:开启“手动设置代理”
profile.set_preference('network.proxy.type',1)
# 第三步:设置代理IP
profile.set_preference('network.proxy.http', proxy[0])
# 第四步:设置代理端口,注意端口是int类型,不是字符串
profile.set_preference('network.proxy.http_port', int(proxy[1])) # 第五步:设置htpps协议也使用该代理
# profile.set_preference('network.proxy.ssl', proxy[0])
# profile.set_preference('network.proxy.ssl_port', proxy[1]) # 第六步:所有协议共用一种 ip 及端口,如果单独配置,不必设置该项,因为其默认为 False
profile.set_preference("network.proxy.share_proxy_settings", True)
# 第七步:设置请求header
profile.set_preference("general.useragent.override", user_agent) # 默认本地地址(localhost)不使用代理,如果有些域名在访问时不想使用代理可以使用类似下面的参数设置
# profile.set_preference("network.proxy.no_proxies_on", "localhost") # 以代理方式启动 firefox
firefox = webdriver.Firefox(profile,executable_path='/opt/geckodriver',options=options) firefox.get('http://www.baidu.com') # 退出
firefox.quit()
二、设置加载超时处理。
1. pageLoadTimeout;
pageLoadTimeout方法用来设置页面完全加载的超时时间,完全加载即页面全部渲染,异步同步脚本都执行完成。没有设置超时时间默认是等待页面全部加载完成才会进入下一步骤,设置超时时间3s是加载到3s时中断操作抛出异常;
driver.manage().timeouts().setScriptTimeout(3,TimeUnit.SECONDS);
2. setScriptTimeout
设置异步脚本到超时时间,用法同pageLoadTimeout一样,异步脚本也就是有async属性的异步脚本,可以在页面解析的同时执行;
(我一般会使用这个解决读取超时问题,用pageLoadTimeout不知道为什么不起作用)
3. implicitlyWait
识别对象的超时时间,如果在设置的时间内没有找到就抛出一个NoSuchElement异常,用法参数和上述一样;
4. driver.set_page_load_timeout
使用selenium爬取人大经济论坛,登陆的时候,页面一直不加载完成,一直在刷新,应该是强制登陆页面一直刷新。
用webdrive的get方法,只能在页面加载完成后才能后续操作,所以,设置了强制加载时间。
这个设置会抛出一个timeout错误,使用pass处理后,继续后边的操作。
后面的操作里又要在webdriver的返回值里查找,
查找的时候,又抛出异常
driver = webdriver.Chrome()
driver.set_page_load_timeout(10) try:
driver.get('http://bbs.pinggu.org/plugin.php?id=dsu_paulsign:sign')
except: # 异常处理
pass # 当页面加载时间超过设定时间,执行后续动作
三:selenium启动Chrome浏览器。
def init_chrom():
display_size = ['800,1200', '1024,600', '1024,768', '1024,1536', '1152,864', '1152,1728', '1280,720',
'1280,768', '1280,800', '1280,900', '1280,960', '1280,1024', '1280,1600', '1280,2048',
'1360,768', '1360,1024', '1400,1050', '1400,2100', '1440,900', '1600,1024', '1600,1200',
'1600,2400', '1680,1050', '1792,1344', '1800,1440']
chrome_options = webdriver.ChromeOptions()
# 在root权限下运行
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# 无界面模式
# chrome_options.add_argument('--headless')
# 解决有时定位会出现问题
chrome_options.add_argument('--disable-gpu')
# 无痕迹模式
chrome_options.add_argument('--incognito')
chrome_options.add_argument('user-agent=' + Ua().random)
# 设置窗口不显示‘自动化测试’提示
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('window-size=' + choice(display_size))
# 禁用自动化栏# 禁用“禁用开发人员模式扩展”弹出窗口
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
chrome_options.add_argument('blink-settings=imagesEnabled=false') # 设置不显示图片
chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 设置不显示图片
driver = webdriver.Chrome(executable_path="/opt/chromedriver", options=chrome_options)
driver.implicitly_wait(10)
# 页面加载最大时间
driver.set_page_load_timeout(20)
driver.get('http://www.baidu.com')
return True
# """
# class属性唯一但是有空格,选择空格两边唯一的那一个
# 若空格隔开的class不唯一可以通过索引进行定位
# self.driver.find_elements_by_class_name('table-dragColumn')[0].click()
# 通过css方法进行定位(空格以‘.’代替)
# options.add_argument('--disable-infobars') # 禁止策略化
# options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在的报错
# options.add_argument('window-size=1920x3000') # 指定浏览器分辨率
# options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
# options.add_argument('--incognito') # 隐身模式(无痕模式)
# options.add_argument('--disable-javascript') # 禁用javascript
# options.add_argument('--start-maximized') # 最大化运行(全屏窗口),不设置,取元素会报错
# options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面
# options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
# options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
# options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" # 手动指定使用的浏览器位置
# options.add_argument('lang=en_US') # 设置语言
# options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36')
# options.add_argument('--headless') # 浏览器不提供可视化页面
# prefs = {"":""}
# prefs["credentials_enable_service"] = False
# prefs["profile.password_manager_enabled"] = False
# chrome_option_set.add_experimental_option("prefs", prefs) # 屏蔽'保存密码'提示框
pyppeteer代替selenium绕过webdriver检测,参考链接:https://www.cnblogs.com/chenhuabin/p/10989895.html
Python selenium + Firefox启动浏览器的更多相关文章
- Python+Selenium自动化-设置浏览器大小、刷新页面、前进和后退
Python+Selenium自动化-设置浏览器大小.刷新页面.前进和后退 1.设置浏览器大小 maximize_window():设置浏览器大小为全屏 set_window_size(500,5 ...
- 爬虫(五)—— selenium模块启动浏览器自动化测试
目录 selenium模块 一.selenium介绍 二.环境搭建 三.使用selenium模块 1.使用chrome并设置为无GUI模式 2.使用chrome有GUI模式 3.查找元素 4.获取标签 ...
- Python+selenium自动化测试之浏览器基础操作
**前言** 本文主要讲解webdriber框架,Selenium 就像真实用户所做的一样,Selenium 测试可以在 Windows.Linux 和 Macintosh上的 Internet ...
- 用Python+selenium打开IE浏览器和Chrome浏览器的问题
这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...
- python selenium模块调用浏览器的时候出错
python selenium模块使用出错,这个怎么改 因为不同版本更新不同步问题,浏览器都要另外下一个驱动.
- Windows上python + selenium + Firefox浏览器的环境配置
1.python安装 我的电脑是32位的,安装了Python 3.5.4版本其它安装版本 2.python环境变量配置 将”C:\Program Files\Python35",”C:\Pr ...
- Python+Selenium学习--启动及关闭浏览器
场景 页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个iframe,如之前iframe介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是div 形式的,这就让我们 ...
- python+selenium实现跨浏览器兼容性测试
python https://www.python.org/ python是一种脚本语言, 易学易用,可以助你快速实现业务逻辑,高效集成系统. ----- http://zh.wikipedia.or ...
- Python+Selenium学习--控制浏览器控制条
场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...
随机推荐
- shell多个脚本怎么并发执行
#!bin/bash PRONUM=10 #进程个数 tmpfile="$$.fifo" #临时生成管道文件 mkfifo $tm ...
- thymeleaf注入springboot
thymeleaf注入springboot需要引入jar: <dependency> <groupId>org.springframework.boot</groupId ...
- TCC细读 - 3 恢复流程
重试定时任务,通过外部调度实现 package org.mengyun.tcctransaction.spring.recover; import org.mengyun.tcctransaction ...
- CSS 表单
输入框前有图片 老板让你实现在输入框前有图片的功能.老板觉得用图片代替文字更有说服力. 要实现这样的功能很简单,它的原理是将图片放在内边距内. 代码 1 2 3 4 5 6 7 8 9 10 11 1 ...
- 商品批量删除(mybatis中集合的使用)
<!-- 根据主键批量删除 --> <delete id="deleteByKeys"> DELETE FROM product WHERE id in & ...
- Linux - 操作系统
操作系统(科普章节) 目标 了解操作系统及作用 1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自 ...
- TPL DataFlow初探(一)
属性TPL Dataflow是微软面向高并发应用而推出的一个类库.借助于异步消息传递与管道,它可以提供比线程池更好的控制,也比手工线程方式具备更好的性能.我们常常可以消息传递,生产-消费模式或Acto ...
- 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表
[抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...
- adb pull 文件夹到电脑
正常来讲是支持文件夹的. 但实际执行的时候发现: pull: building file list...0 files pulled. 0 files skipped. 其实出现这个提示,归根到底还是 ...
- sqlserver 组内排序
关键词 partition(分区函数[pɑː'tɪʃ(ə)n]) by 参考: https://www.cnblogs.com/sanlang/archive/2009/03/24/1420360. ...