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 ...
随机推荐
- React更新元素 基础
React元素创建后无法修改其内容和属性.唯一的办法是创建新的元素,传入ReactDOM.render()方法 三种实现形式: 1.整体替换 function tick () { const ele= ...
- Kettle安装Kafka Consumer和Kafka Producer插件
1.从github上下载kettle的kafka插件,地址如下 Kafka Consumer地址: https://github.com/RuckusWirelessIL/pentaho-kafka- ...
- ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类
ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:读取文本文件,常见多边形要素 ...
- 十四、ChainOfResponsibility 责任链模式
设计: 代码清单: Trouble: public class Trouble { private int number; public Trouble(int number){ this.numbe ...
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- python—切片
切片就是list取值的一种方式 l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(l[1:5]) #取值方式顾头不顾尾 print(l[:5]) #冒号 前面 没写代 ...
- 交叉编译sudo
编译Sudo version 1.8.6p7下载路径:https://www.sudo.ws/news.html 1.交叉编译 # tar -xvf sudo-1.8.6p7.tar.gz # cd ...
- 使用jmail发送短信
原文链接 https://blog.csdn.net/sdaujsj1/article/details/79248469 pom <!-- https://mvnrepository.com/a ...
- spring mvc mybatis shiro构建cms系统ios android
开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...
- unbuntu 安装 teamviewer
下载 teamviewer 安装包 使用 dpkg 安装 deb 安装包 使用 sudo apt-get install -f 解决依赖问题