Selenium笔记(2)Chrome Webdriver启动选项

本文集链接:https://www.jianshu.com/nb/25338984

Selenium中使用不同的Webdriver可能会有不一样的方法,有些相同的操作会得到不一样的结果,本文主要介绍的是Chrome()的使用方法。

其他Webdriver可以查阅官方文档。

Chrome WebDriver Options

简介

这是一个Chrome的参数对象,在此对象中使用add_argument()方法可以添加启动参数,添加完毕后可以在初始化Webdriver对象时将此Options对象传入,则可以实现以特定参数启动Chrome。

例子

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

# 实例化一个启动参数对象
chrome_options = Options()
# 添加启动参数
chrome_options.add_argument('--window-size=1366,768')
# 将参数对象传入Chrome,则启动了一个设置了窗口大小的Chrome
browser = webdriver.Chrome(chrome_options=chrome_options)

常用的启动参数

启动参数 作用
--user-agent="" 设置请求头的User-Agent
--window-size=1366,768 设置浏览器分辨率
--headless 无界面运行
--start-maximized 最大化运行
--incognito 隐身模式
--disable-javascript 禁用javascript
--disable-infobars 禁用浏览器正在被自动化程序控制的提示

完整启动参数可以到此页面查看:

https://peter.sh/experiments/chromium-command-line-switches/

禁用图片加载

Chrome的禁用图片加载参数设置比较复杂,如下所示:

prefs = {
   'profile.default_content_setting_values' : {
       'images' : 2
  }
}
options.add_experimental_option('prefs',prefs)

禁用浏览器弹窗

使用浏览器时常常会有弹窗弹出,以下选项可以禁止弹窗:

prefs = {  
   'profile.default_content_setting_values' : {  
       'notifications' : 2  
    }  
}  
options.add_experimental_option('prefs',prefs)

完整文档

class selenium.webdriver.chrome.options.Options

Bases: object

Method

  • __init__()

  • add_argument(argument)

    Adds an argument to the listArgs:Sets the arguments

  • add_encoded_extension(extension)

    Adds Base64 encoded string with extension data to a list that will be used to extract it to the ChromeDriverArgs:extension: Base64 encoded string with extension data

  • add_experimental_option(name, value)

    Adds an experimental option which is passed to chrome.Args:name: The experimental option name. value: The option value.

  • add_extension(extension)

    Adds the path to the extension to a list that will be used to extract it to the ChromeDriverArgs:extension: path to the *.crx file

  • set_headless(headless=True)

    Sets the headless argumentArgs:headless: boolean value indicating to set the headless option

  • to_capabilities()

    Creates a capabilities with all the options that have been set andreturns a dictionary with everything

Values

  • KEY = 'goog:chromeOptions'

  • arguments

    Returns a list of arguments needed for the browser

  • binary_location

    Returns the location of the binary otherwise an empty string

  • debugger_address

    Returns the address of the remote devtools instance

  • experimental_options

    Returns a dictionary of experimental options for chrome.

  • extensions

    Returns a list of encoded extensions that will be loaded into chrome

  • headless

    Returns whether or not the headless argument is set

Chrome WebDriver对象

简介

这个对象继承自selenium.webdriver.remote.webdriver.WebDriver,这个类会在下一章讲到,Chrome的WebDriver作为子类增添了几个方法。

指定chromedriver.exe的位置

chromedriver.exe一般可以放在环境文件中,但是有时候为了方便部署项目,或者为了容易打包,我们可以将chromedriver.exe放到我们的项目目录中,然后在初始化Chrome Webdriver对象时,传入chromedriver.exe的路径。

如下所示:

from selenium import webdriver
browser = webdriver.Chrome(executable_path='chromedriver.exe')

完整文档

class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, options=None, service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=None)

Bases: selenium.webdriver.remote.webdriver.WebDriver

Controls the ChromeDriver and allows you to drive the browser.

You will need to download the ChromeDriver executable fromhttp://chromedriver.storage.googleapis.com/index.html

  • __init__(executable_path='chromedriver', port=0, options=None, service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=None)

    Creates a new instance of the chrome driver.

    Starts the service and then creates new instance of chrome driver.

    Args:

    • executable_path - path to the executable. If the default is used it assumes the executable is in the $PATHport

    • port you would like the service to run, if left as 0, a free port will be found.

    • desired_capabilities: Dictionary object with non-browser specific capabilities only, such as “proxy” or “loggingPref”.

    • options: this takes an instance of ChromeOptions

  • create_options()

  • get_network_conditions()

    Gets Chrome network emulation settings.

    Returns:A dict. For example:

    {‘latency’: 4, ‘download_throughput’: 2, ‘upload_throughput’: 2, ‘offline’: False}

  • launch_app(id)

    Launches Chrome app specified by id.

  • quit()

    Closes the browser and shuts down the ChromeDriver executable that is started when starting the ChromeDriver

  • set_network_conditions(**network_conditions)

    Sets Chrome network emulation settings.

    Args:

    • network_conditions: A dict with conditions specification.

    Usage:

    driver.set_network_conditions(offline=False, latency=5, # additional latency (ms)
                                 download_throughput=500 * 1024, # maximal throughput
                                 upload_throughput=500 * 1024) # maximal throughput

    Note: ‘throughput’ can be used to set both (for download and upload).

python爬虫基础08-selenium大全2/8-Chrome Webdriver启动选项的更多相关文章

  1. Python 爬虫的工具列表大全

    Python 爬虫的工具列表大全 这个列表包含与网页抓取和数据处理的Python库.网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pyc ...

  2. Python爬虫基础

    前言 Python非常适合用来开发网页爬虫,理由如下: 1.抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁:相比其他动态脚本语言,如perl ...

  3. python爬虫动态html selenium.webdriver

    python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...

  4. python爬虫-基础入门-python爬虫突破封锁

    python爬虫-基础入门-python爬虫突破封锁 >> 相关概念 >> request概念:是从客户端向服务器发出请求,包括用户提交的信息及客户端的一些信息.客户端可通过H ...

  5. python爬虫-基础入门-爬取整个网站《3》

    python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...

  6. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...

  7. python爬虫-基础入门-爬取整个网站《1》

    python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...

  8. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  9. PYTHON 爬虫笔记七:Selenium库基础用法

    知识点一:Selenium库详解及其基本使用 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium ...

随机推荐

  1. vue 脚手架 图片预加载

    $('.back-img').onload =function(){ var img =new Image(); img.src = '../assets/dt-bj.png'; } 该组件内的图片加 ...

  2. 基于.net core微服务(Consul、Ocelot、Docker、App.Metrics+InfluxDB+Grafana、Exceptionless、数据一致性、Jenkins)

    1.微服务简介 一种架构模式,提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相沟通(RESTfu ...

  3. 表单辅助函数-form_open()

    使用from_open()之前需要装载本辅助函数: $this->load->helper('form'); php===> echo form_open('email/send') ...

  4. [转][android][利用JNI技术在Android中调用、调试C++代码]

    在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...

  5. 借助sass的Maps功能使得响应式代码更有条理

    原文来自这里 本文综合了原文(by Jonathan Suh)以及笔者自己的理解. Introduction 众所周知,写代码与写维护性高的代码是两回事.而涉及到响应式,代码又特别容易变的杂乱.借助s ...

  6. 解决部分浏览器不能显示itext生成的PDF文件文本域内容问题

    利用Itext可以实现pdf的高效动态生成,但在实践过程中遇到了一个问题: 即itext利用map中的值设置到pdf模板上建立的文本域中时:能成功生成,但是在部分浏览器上(360,QQ,等浏览器)无法 ...

  7. WebService学习之旅(四)Apache Axis2的安装

    一.Axis2简介 Axis2是目前使用较多的WebService引擎,它是Axis1.x的升级版本,不仅支持SOAP1.1和SOAP1.2,而且也提供了对REST风格WebService的支持. A ...

  8. Android adb命令,linux中各种命令

    常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...

  9. (九)maven之聚合多模块

    聚合项目 一些开源项目,都会把自己的源代码公开到github之类的网站上,我们通过下载其代码,在本地执行maven install,可以把代码编译成jar包安装到本地仓库.而一个项目通常有多个模块,比 ...

  10. 迅为4412开发板Linux设备树的镜像烧写和源码简单优化教程

    1 烧写:   烧写和4412默认镜像的烧写类似,使用fastboot. 先更新uboot,用4412默认uboot更新支持设备树的uboot 用支持设备树的uboot烧写. 进入支持设备树的uboo ...