selenium, we will talk about these contents in general
selenium
Introduction and installationPositioning of page elements Browser Control Mouse control Keyboard control -
Set element wait Get cookies
Call JavaScript
selenium
Advanced
Introduction and installation of selenium
selenium
is the most widely used open source Web UI One of the automated test suites, the languages it supports include C
, Java
, Perl
, PHP
, Python
和Ruby
is also a powerful tool in data capture, which can solve the anti-crawling measures of most web pages. Of course, it is not omnipotent. One of the more obvious points is that it is relatively slow. , if the amount of data collected every day is not very high, you can use this framework. So when it comes to installation, you can directly use pip
to install
pip install selenium
At the same time, we also need to install a browser driver. Different browsers need to be installed. Different drivers, the editor here mainly recommends the following two
Firefox
Browser driver:geckodriver
##Chrome Browser driver:
chromedriver
chromedriver, so here I will use
Chrome Browser is an example. Since the version of
chromedriver needs to be consistent with the version of the browser, let’s first confirm what the version of the browser is? Look at the picture below
, of course it must also correspond to your computer’s operating system
Positioning of page elements
When talking about the positioning of page elements, the editor assumes that readers have the most basic front-end knowledge, such asHTML
, CSS
, etc.
ID标签的定位
HTML
当中,ID属性是唯一标识一个元素的属性,因此在selenium
当中,通过ID来进行元素的定位也作为首选,我们以百度首页为例,搜索框的HTML
代码如下,其ID为“kw”,而“百度一下”这个按钮的ID为“su”,我们用Python
脚本通过ID的标签来进行元素的定位driver.find_element_by_id("kw") driver.find_element_by_id("su")
NAME标签的定位
HTML
当中,Name属性和ID属性的功能基本相同,只是Name属性并不是唯一的,如果遇到没有ID标签的时候,我们可以考虑通过Name标签来进行定位,代码如下driver.find_element_by_name("wd")
Xpath定位
Xpath
方式来定位几乎涵盖了页面上的任意元素,那什么是Xpath
呢?Xpath
是一种在XML
和HTML
文档中查找信息的语言,当然通过Xpath
路径来定位元素的时候也是分绝对路径和相对路径。/
来表示,相对路径是以//
来表示,而涉及到Xpath
路径的编写,小编这里偷个懒,直接选择复制/粘贴的方式,例如针对下面的HTML
代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test</title> </head> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> </html>
Xpath
来进行页面元素的定位,代码如下driver.find_element_by_xpath('//*[@id="kw"]')
className标签定位
class
属性来定位元素,尤其是当我们看到有多个并列的元素如list
表单,class
用的都是共用同一个,如:driver.find_element_by_class_name("classname")
class
属性来定位元素,该方法返回的是一个list
列表,而当我们想要定位列表当中的第n个元素时,则可以这样来安排driver.find_elements_by_class_name("classname")[n]
find_elements_by_class_name()
方法而不是find_element_by_class_name()
方法,这里我们还是通过百度首页的例子,通过className
标签来定位搜索框这个元素driver.find_element_by_class_name('s_ipt')
CssSelector()
方法定位
Selenium
官网当中是更加推荐CssSelector()
方法来进行页面元素的定位的,原因在于相比较于Xpath
定位速度更快,Css
定位分为四类:ID值、Class
属性、TagName
值等等,我们依次来看ID方式来定位
TagName
的值,另外一种则是不加,代码如下driver.find_element_by_css_selector("#id_value") # 不添加前面的`TagName`值 driver.find_element_by_css_selector("tag_name.class_value") # 不添加前面的`TagName`值
TagName
的值非常的冗长,中间可能还有空格,那么这当中的空格就需要用点“.”来替换driver.find_element_by_css_selector("tag_name.class_value1.calss_value2.class_value3") # 不添加前面的`TagName`值
我们仍然以百度首页的搜索框为例,它的HTML
代码如下
CssSelector
的.class()
方式来实现元素的定位的话,Python
代码该这样来实现,和上面Xpath()
的方法一样,可以稍微偷点懒,通过复制/粘贴的方式从开发者工具当中来获取元素的位置代码如下
driver.find_element_by_css_selector('#kw')
linkText()方式来定位
这个方法直接通过链接上面的文字来定位元素,案例如下
通过linkText()
方法来定位“地图”这个元素,代码如下
driver.find_element_by_link_text("地图").click()
浏览器的控制
修改浏览器窗口的大小
set_window_size()
这个方法来修改浏览器窗口的大小,代码如下# 修改浏览器的大小 driver.set_window_size(500, 900)
同时还有maxmize_window()
方法是用来实现浏览器全屏显示,代码如下
# 全屏显示 driver.maximize_window()
浏览器的前进与后退
前进与后退用到的方法分别是forward()
和back()
,代码如下
# 前进与后退 driver.forward() driver.back()
浏览器的刷新
刷新用到的方法是refresh()
,代码如下
# 刷新页面 driver.refresh()
除了上面这些,webdriver
的常见操作还有
关闭浏览器: get()
清除文本: clear()
单击元素: click()
提交表单: submit()
模拟输入内容: send_keys()
我们可以尝试着用上面提到的一些方法来写段程序
from selenium import webdriver from time import sleep driver = webdriver.Chrome(executable_path="chromedriver.exe") driver.get("https://www.baidu.com") sleep(3) driver.maximize_window() sleep(1) driver.find_element_by_xpath('//*[@id="s-top-loginbtn"]').click() sleep(3) driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__userName"]').send_keys('12121212') sleep(1) driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__password"]').send_keys('testtest') sleep(2) driver.refresh() sleep(3) driver.quit()
output
鼠标的控制
鼠标的控制都是封装在ActionChains
类当中,常见的有以下几种
引入action_chains类 from selenium.webdriver.common.action_chains import ActionChains # 右击 ActionChains(driver).context_click(element).perform() # 双击 ActionChains(driver).double_click(element).perform() # 拖放 ActionChains(driver).drag_and_drop(Start, End).perform() # 悬停 ActionChains(driver).move_to_element(Above).perform() # 按下 ActionChains(driver).click_and_hold(leftclick).perform() # 执行指定的操作
键盘的控制
webdriver
中的Keys()
类,提供了几乎所有按键的方法,常用的如下
# 删除键 driver.find_element_by_id('xxx').send_keys(Keys.BACK_SPACE) # 空格键 driver.find_element_by_id('xxx').send_keys(Keys.SPACE) # 回车键 driver.find_element_by_id('xxx').send_keys(Keys.ENTER) # Ctrl + A 全选内容 driver.find_element_by_id('xxx').send_keys(Keys.CONTROL, 'a') # Ctrl + C/V 复制/粘贴内容 driver.find_element_by_id('xxx').send_keys(Keys.CONTROL, 'c') driver.find_element_by_id('xxx').send_keys(Keys.CONTROL, 'v')
其他的一些键盘操作
向上箭头: Keys.ARROW_UP
向下箭头: Keys.ARROW_DOWN
向左/向右箭头: Keys.ARROW_LEFT
/Keys.ARROW_RIGHT
Shift键: Keys.SHIFT
F1键: Keys.F1
元素的等待
有显示等待和隐式等待两种
显示等待
TimeoutException
),需要用到的是WebDriverWait()
方法,同时配合until
和not until
方法WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
其中的参数:
timeout: 最长超时时间,默认以秒为单位 poll_frequency: 检测的时间间隔,默认是0.5s ignored_exceptions: 指定忽略的异常,默认忽略的有 NoSuchElementException
这个异常
我们来看下面的案例
driver = webdriver.Chrome() driver.get("http://somedomain/url_that_delays_loading") try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement"))) finally: driver.quit()
隐式等待
主要使用的是implicitly_wait()
来实现
browser = webdriver.Chrome(path) # 隐式等待3秒 browser.implicitly_wait(3)
获取Cookie
Cookie
是用来识别用户身份的关键,我们通常也是通过selenium
先模拟登录网页获取Cookie
,然后再通过requests
携带Cookie
来发送请求。webdriver
提供了cookies
的几种操作,我们挑选几个常用的来说明
get_cookies()
:以字典的形式返回当前会话中可见的cookie
信息get_cookies(name)
: 返回cookie
字典中指定的的cookie
信息add_cookie(cookie_dict)
: 将cookie
添加到当前会话中
下面看一个简单的示例代码
driver=webdriver.Chrome(executable_path="chromedriver.exe") driver.get(url=url) time.sleep(1) cookie_list=driver.get_cookies() cookies =";".join([item["name"] +"=" + item["value"] + "" for item in cookie_list]) session=requests.session() headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36', 'cookie': cookies } response=session.get(url=url,headers=headers) soup=BeautifulSoup(response.text,'lxml')
调用JavaScript
webdriver
当中可以使用execut_script()
方法来实现JavaScript
的执行,下面我们来看一个简单的例子from selenium import webdriver import time bro=webdriver.Chrome(executable_path='./chromedriver') bro.get("https://www.baidu.com") # 执行js代码 bro.execute_script('alert(10)') time.sleep(3) bro.close()
除此之外,我们还可以通过selenium
执行JavaScript
来实现屏幕上下滚动
from selenium import webdriver bro=webdriver.Chrome(executable_path='./chromedriver') bro.get("https://www.baidu.com") # 执行js代码 bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
selenium
进阶
selenium
启动的浏览器,会非常容易的被检测出来,通常可以通过window.navigator.webdriver
的值来查看,如果是true
则说明是使用了selenium
模拟浏览器,如果是undefined
则通常会被认为是正常的浏览器。window.navigator.webdriver
最后返回的值driver.execute_script( 'Object.defineProperties(navigator,{webdriver:{get:()=>false}})' )
JavaScript
程序已经通过读取window.navigator.webdriver
知道你使用的是模拟浏览器了。所以我们有两种办法来解决这个缺陷。在Chrome当中添加实验性功能参数
代码如下
from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = ChromeOptions() option.add_experimental_option('excludeSwitches',['enable-automation']) driver=Chrome(options=option)
调用chrome当中的开发工具协议的命令
Chrome
浏览器在打开页面,还没有运行网页自带的JavaScript
代码时,先来执行我们给定的代码,通过execute_cdp_cmd()
方法,driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ })
当然为了更好隐藏指纹特征,我们可以将上面两种方法想结合
from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path='./chromedriver') driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) driver.get(url)
stealth.min.js
文件来实现隐藏selenium
模拟浏览器的特征,这个文件之前是给puppeteer
用的,使得其隐藏浏览器的指纹特征,而让Python
使用时,需要先导入这份JS
文件import time from selenium.webdriver import Chrome option = webdriver.ChromeOptions() option.add_argument("--headless") # 无头浏览器需要添加user-agent来隐藏特征 option.add_argument('user-agent=.....') driver = Chrome(options=option) driver.implicitly_wait(5) with open('stealth.min.js') as f: js = f.read() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": js }) driver.get(url)
The above is the detailed content of Tips | Python crawler tool Selenium from entry to advanced. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor