原理与环境配置
原理
Selenium 是一套 Web 网站的程序自动化操作解决方案。
通过它,我们可以写出自动化程序,像人一样在浏览器里操作 web 界面。 比如点击界面按钮,在文本框中输入文字等操作。而且还能从 web 界面获取信息。 比如获取火车、汽车票务信息,招聘网站职位信息,财经网站股票价格信息等等,然后用程序进行分析处理。
Selenium 的自动化原理是这样的:
从上图可以看出:我们写的自动化程序需要使用 客户端库,程序的自动化请求都是通过这个库里面的编程接口发送给浏览器。
比如,我们要模拟用户点击界面按钮, 自动化程序里面就应该调用客户端库相应的函数,就会发送 点击元素 的请求给下方的 浏览器驱动。 然后,浏览器驱动再转发这个请求给浏览器。
这个自动化程序发送给浏览器驱动的请求是 HTTP请求。
客户端库从哪里来的? 是Selenium组织提供的。Selenium 组织提供了多种编程语言的 Selenium 客户端库, 包括 java,python,js,ruby 等,方便不同编程语言的开发者使用。我们只需要安装好客户端库,调用这些库,就可以发出自动化请求给浏览器了。
1
2
3
4
5
6
7
8
9
10
11
|
浏览器驱动也是一个独立的程序,是由浏览器厂商提供的,不同的浏览器需要不同的浏览器驱动。
比如Chrome浏览器和火狐浏览器有各自不同的驱动程序。
浏览器驱动接收到我们的自动化程序发送的界面操作请求后,会转发请求给浏览器,
让浏览器去执行对应的自动化操作。
浏览器执行完操作后,会将自动化的结果返回给浏览器驱动,
浏览器驱动再通过HTTP响应的消息返回给我们的自动化程序的客户端库。
自动化程序的客户端库接收到响应后,将结果转化为数据对象返回给我们的代码。
我们的程序就可以知道这次自动化操作的结果如何了。
|
selenium 自动化流程如下:
-
自动化程序调用 Selenium 客户端库函数(比如点击按钮元素)。
-
客户端库会发送 Selenium 命令给浏览器的驱动程序。
-
浏览器驱动程序接收到命令后,驱动浏览器去执行命令。
-
浏览器执行命令。
-
浏览器驱动程序获取命令执行的结果,返回给我们自动化程序。
-
自动化程序对返回结果进行处理。
环境配置
安装 python 和 pycharm
python 下载地址:https://www.python.org/,下载成功后,双击安装程序,开始安装。
在命令行中直接运行 python,出现以下类似的代码则表示安装成功。
Pycharm 下载地址:https://www.jetbrains.com/pycharm/
安装 Selenium 客户端库
不同的编程语言选择不同的Selenium客户端库。
对应我们 Python 语言来说,Selenium客户端库的安装非常简单,用 pip 命令即可。
打开命令行程序,运行如下命令:
如果安装不了,可能是网络问题,可以指定使用国内的清华大学源。
1
|
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
|
安装 Chrome 浏览器
浏览器驱动 是和 浏览器对应的。不同的浏览器需要选择不同的浏览器驱动。
目前主流的浏览器中,谷歌 Chrome 浏览器对 Selenium 自动化的支持更加成熟一些。
可以点击这里,下载安装谷歌浏览器
安装 Chrome 浏览器驱动
Chrome 浏览器安装好以后,接下来就是安装浏览器驱动 Chrome Driver
。
点击这里: Chrome driver 官方下载地址,选择当前的版本的浏览器对应的驱动。
安装 Edge 浏览器驱动
如果你选择微软 Edge浏览器做自动化,只需要安装 Edge浏览器驱动,如下:
点击这里打开Edge浏览器驱动下载页面,选择在 Stable Channel 下载(下图箭头处)
根据你的电脑运行的平台,选择对应的链接,
比如:如果你的电脑是 x86架构的CPU(目前的AMD,Intel CPU 都是x86架构),运行的 Windows系统,现在Windows操作系统基本都是64位,就选择 x64
。 x86
链接对应 32位的浏览器驱动,其实也可以,因为 Windows 操作系统目前都兼容运行32位的程序。
selenium 基础
简单示例
通过代码自动打开 Chrome 浏览器,浏览百度网站。
1
2
3
4
5
6
7
8
9
10
11
12
|
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(service=Service(r'E:\chromedriver-win64\chromedriver.exe'))
# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.baidu.com')
# 程序运行完会自动关闭浏览器,就是很多人说的闪退
# 这里加入等待用户输入,防止闪退
input('等待回车键结束程序')
|
其中,第 5 行代码,就会运行浏览器驱动,并且运行Chrome浏览器
1
|
wd = webdriver.Chrome(service=Service(r'd:\tools\chromedriver.exe'))
|
注意,等号右边返回的是 WebDriver 类型的对象,我们可以通过这个对象来操控浏览器,比如打开网址、选择界面元素等。
而第 8 行代码,就是使用 WebDriver 的 get 方法打开网址百度。
1
|
wd.get('https://www.baidu.com')
|
执行上面这行代码时,自动化程序就发起了打开百度网址的请求消息 ,通过浏览器驱动, 给 Chrome 浏览器。
Chome 浏览器接收到该请求后,就会打开百度网址,通过浏览器驱动,告诉自动化程序打开成功。
注意,执行完自动化代码,如果想关闭浏览器窗口可以调用 WebDriver 对象的 quit 方法,像这样 wd.quit()。
省略浏览器驱动路径
前面,我们的代码创建 WebDriver对象时,需要指定浏览器驱动路径,比如
1
2
|
from selenium.webdriver.chrome.service import Service
wd = webdriver.Chrome(service=Service(r'd:\tools\chromedriver.exe'))
|
我们可以把浏览器驱所在目录加入环境变量 Path
, 写代码时,就可以无需指定浏览器驱动路径了。
1
|
wd = webdriver.Chrome()
|
selenium 选择元素
根据 id 选择元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from selenium import webdriver
from selenium.webdriver.common.by import By
# 创建 WebDriver 对象
wd = webdriver.Chrome()
wd.get('https://www.baidu.com')
# 根据id选择元素,返回的就是该元素对应的WebElement对象
element = wd.find_element(By.ID, 'kw')
# 通过该 WebElement对象,就可以对页面元素进行操作了
# 比如输入字符串到这个输入框里
element.send_keys('三一')
# 点击搜索按钮
wd.find_element(By.ID, 'su').click()
# 加入等待用户输入,防止闪退
input()
|
其中,element = wd.find_element(By.ID, ‘kw’) 使用了 WebDriver 对象的 find_element 方法,这行代码运行时,会发起一个请求通过浏览器驱动转发给浏览器,让浏览器选择一个 id 为 kw 的元素。
浏览器找到 id 为 kw 的元素后,将结果通过浏览器驱动返回给自动化程序,find_element 方法会返回一个 WebElement 类型的对象。
这个 WebElement 对象可以看成是对应页面元素的遥控器,通过这个WebElement对象,就可以操控对应的界面元素。
根据 class 或 tagName 选择元素
WebDriver 实例对象的 get 方法可以让浏览器打开指定网址,打开 VScode,创建 html 页面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.animal{
color: red;
}
</style>
</head>
<body>
<div class="plant"><span>土豆</span></div>
<div class="plant"><span>洋葱</span></div>
<div class="plant"><span>白菜</span></div>
<div class="animal"><span>狮子</span></div>
<div class="animal"><span>老虎</span></div>
<div class="animal"><span>山羊</span></div>
</body>
<script>
console.log(document.getElementsByClassName('animal')[0])
</script>
</html>
|
在文件夹中找到该页面文件所在的位置,使用 wd.get 打开。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from selenium import webdriver
from selenium.webdriver.common.by import By
# 创建 WebDriver 实例对象,指明使用 chrome 浏览器驱动
wd = webdriver.Chrome()
# WebDriver 实例对象的 get 方法可以让浏览器打开指定网址
wd.get('D:\VSCodeProjects\VSCodeP1\h4.html')
# 根据 class name 选择元素,返回的是 一个列表
# 里面 都是 class 属性值为 animal 的元素对应的 WebElement 对象
elements = wd.find_elements(By.CLASS_NAME, 'animal')
spans = wd.find_elements(By.TAG_NAME, 'span')
print(animals)
print(spans)
# 取出列表中的每个 WebElement 对象,打印出其 text 属性的值
# text 属性就是该 WebElement 对象对应的元素在网页中的文本内容
for i in animals: print(i.text)
print('--------------------')
for i in spans: print(i.text)
|
使用 find_elements 选择符合条件的所有元素,如果没有符合条件的元素,返回空列表。
使用 find_element 选择符合条件的第一个元素,如果没有符合条件的元素,抛出异常。
根据 webElement 选择元素
不仅 WebDriver 对象有选择元素 的方法, WebElement对象也有选择元素的方法。
WebElement 对象也可以调用 find_elements, find_element 之类的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.animal{
color: red;
}
</style>
</head>
<body>
<div class="plant"><span>土豆</span></div>
<div class="plant"><span>洋葱</span></div>
<div class="plant"><span>白菜</span></div>
<div class="animal"><span>狮子</span></div>
<div class="animal"><span>老虎</span></div>
<div class="animal"><span>山羊</span></div>
<div class="container">
<div class="animal"><span>豹子</span></div>
<div class="animal"><span>大象</span></div>
<div class="animal"><span>河马</span></div>
</div>
</body>
<script>
console.log(document.getElementsByClassName('animal')[0])
</script>
</html>
|
WebDriver 对象选择元素的范围是整个 web 页面, 而 WebElement 对象选择元素的范围是该元素的内部。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.get('D:\VSCodeProjects\VSCodeP1\h4.html')
# animals = wd.find_elements(By.CLASS_NAME, 'animal')
# for i in animals: print(i.text)
container = wd.find_element(By.CLASS_NAME, 'container')
containerAnimals = container.find_elements(By.TAG_NAME, 'span')
print(containerAnimals)
for i in containerAnimals: print(i.text)
|
selenium 隐式等待
1
2
3
4
5
6
7
8
9
10
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')
wd.find_element(By.ID, 'kw').send_keys('长沙')
wd.find_element(By.ID, 'go').click()
print(wd.find_element(By.ID, '1').text)
wd.quit()
|
在我们进行网页操作的时候,有的元素内容不是可以立即出现的,可能会等待一段时间。
比如我们点击搜索后,浏览器需要把这个搜索请求发送给服务器,服务器进行处理后,再把搜索结果返回给我们。
所以,从点击搜索到得到结果,需要一定的时间,只是通常服务器的处理比较快,我们感觉好像是立即出现了搜索结果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')
wd.find_element(By.ID, 'kw').send_keys('长沙')
wd.find_element(By.ID, 'go').click()
time.sleep(1)
print(wd.find_element(By.ID, '1').text)
# while True:
# try:
# print(wd.find_element(By.ID, '1').text)
# break
# except:
# time.sleep(1)
wd.quit()
|
Selenium 的 Webdriver 对象有个方法叫 implicitly_wait
,可以称之为隐式等待,或者 全局等待。
该方法接受一个参数,用来指定最大等待时长,如果我们加入如下代码:
那么后续所有的 find_element
或者 find_elements
之类的方法调用都会采用以下策略:如果找不到元素, 每隔半秒钟再去界面上查看一次,直到找到该元素或者超过最大时长。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.implicitly_wait(10)
wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')
wd.find_element(By.ID, 'kw').send_keys('长沙')
wd.find_element(By.ID, 'go').click()
print(wd.find_element(By.ID, '1').text)
wd.quit()
|
selenium 操作元素
点击元素
输入字符串就是调用元素 WebElement 对象的 click 方法
1
2
|
element = wd.find_element(By.ID, 'go')
element.click()
|
输入字符串
输入字符串就是调用元素 WebElement 对象的 send_keys 方法,如果要输入的输入框中已有字符,则需要使用 clear 方法清除内容。
1
2
3
|
element = wd.find_element(By.ID, 'kw')
element.clear()
element.send_keys('长沙')
|
获取元素信息
获取元素的文本内容
通过 WebElement 对象的 text 属性,可以获取元素展示在界面上的文本内容。
1
2
|
element = wd.find_element(By.ID, 'animal')
print(element.text)
|
获取元素属性
通过 WebElement 对象的 get_attribute 方法来获取元素的属性值。
比如要获取元素属性 class 的值,就可以使用 element.get_attribute(‘class’)。
1
2
|
element = wd.find_element(By.ID, 'input_name')
print(element.get_attribute('class'))
|
获取整个元素对应的 HTML
获取整个元素对应的 HTML 文本内容,可以使用 element.get_attribute(‘outerHTML’)。
获取某个元素内部的 HTML 文本内容,可以使用 element.get_attribute(‘innerHTML’)。
获取输入框里面的文字
对于 input 输入框的元素,要获取里面的输入文本,用 text 属性是不行的,这时可以使用 element.get_attribute(‘value’)。
1
2
|
element = wd.find_element(By.ID, "input1")
print(element.get_attribute('value')) # 获取输入框中的文本
|
获取元素文本内容
通过 WebElement 对象的 text 属性,可以获取元素展示在界面上的文本内容。
但是有时候元素的文本内容没有展示在界面上,或者没有完全完全展示在界面上。 这时,用 WebElement 对象的 text 属性获取文本内容就会有问题。
可以使用 element.get_attribute(‘innerText’) 或者 element.get_attribute(’textContent’)。
使用 innerText 和 textContent 的区别是,前者只显示元素可见文本内容,后者显示所有内容(包括display属性为none的部分)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')
# 获取元素的文本内容
print(wd.find_element(By.TAG_NAME, 'nav').text)
# 获取元素属性
print(wd.find_elements(By.TAG_NAME, 'input')[0].get_attribute('style'))
# 获取整个元素对应的 HTML
print(wd.find_elements(By.TAG_NAME, 'input')[0].get_attribute('outerHTML'))
print(wd.find_elements(By.CLASS_NAME, 'result-item')[0].find_elements(By.TAG_NAME, 'p')[1].get_attribute('innerHTML'))
# 获取输入框里面的文字
print(wd.find_elements(By.TAG_NAME, 'input')[0].get_attribute('value'))
# 获取元素文本内容
print(wd.find_element(By.TAG_NAME, 'strong').get_attribute('innerText'))
print(wd.find_element(By.TAG_NAME, 'strong').get_attribute('textContent'))
wd.quit()
|
CSS 表达式
根据 tagName、id、class 选择元素
通过 CSS Selector 选择元素的方法是:
1
2
3
4
|
# 选择单个元素
find_element(By.CSS_SELECTOR, CSS Selector参数)
# 选择所有元素
find_elements(By.CSS_SELECTOR, CSS Selector参数)
|
要选择 所有的 tagName 为 div 的元素,就可以是这样:
1
2
3
|
elements = wd.find_elements(By.CSS_SELECTOR, 'div')
# 等价于
elements = wd.find_elements(By.TAG_NAME, 'div')
|
以百度为例,通过 CSS Selector 选择元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.implicitly_wait(3)
wd.get('https://www.baidu.com')
# 通过 tagName 获取
print(wd.find_elements(By.CSS_SELECTOR, 'meta')[0].get_attribute('content'))
# 通过 class 获取
print(wd.find_elements(By.CSS_SELECTOR, '.title-content-title')[0].get_attribute('innerHTML'))
# 通过 id 获取
print(wd.find_element(By.CSS_SELECTOR, '#s-top-left').find_elements(By.CSS_SELECTOR, '.mnav')[0].text)
wd.quit()
|
选择子元素和后代元素
如果元素2是元素1的直接子元素, CSS Selector 选择后代元素的语法为:
如果元素2是元素1的后代元素, CSS Selector 选择后代元素的语法为:
创建 html 页面如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id='container'>
<div id='layer1'>
<div id='inner11'>
<span>内层11</span>
</div>
<div id='inner12'>
<span>内层12</span>
</div>
</div>
<div id='layer2'>
<div id='inner21'>
<span>内层21</span>
</div>
</div>
</div>
<a href="http://www.baidu.com">百度一下</a>
</body>
</html>
|
在该页面中选中 span 标签下的内层11。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.implicitly_wait(3)
wd.get('D:\VSCodeProjects\VSCodeP1\h4.html')
# 直接子元素
print(wd.find_element(By.CSS_SELECTOR, '#container > #layer1 > #inner11 > span').text)
# 后代元素
print(wd.find_element(By.CSS_SELECTOR, '#container #inner11').text)
wd.quit()
|
根据属性选择元素
css 选择器支持通过任何属性来选择元素,语法是用一个方括号 []。
1
2
|
a = wd.find_element(By.CSS_SELECTOR, '[href="https://www.baidu.com"]')
print(a.get_attribute('outerHTML'))
|
当然,前面可以加上标签名的限制,比如 div[class=‘SKnet’] 表示选择所有标签名为 div,且 class 属性值为 SKnet 的元素。
属性值用单引号,双引号都可以。
根据属性选择,还可以不指定属性值,比如 [href] , 表示选择所有具有属性名为 href 的元素,不管它们的值是什么。
CSS 还可以选择属性值包含某个字符串的元素。
比如,要选择 a 节点,里面的 href 属性包含了 baidu 字符串,就可以这样写:
1
2
|
a = wd.find_element(By.CSS_SELECTOR, 'a[href*="baidu"]')
print(a.get_attribute('outerHTML'))
|
还可以选择属性值以某个字符串开头的元素。
比如,要选择 a 节点,里面的 href 属性以 https 开头,就可以这样写:
1
2
|
a = wd.find_element(By.CSS_SELECTOR, 'a[href^="https"]')
print(a.get_attribute('outerHTML'))
|
还可以选择属性值以某个字符串结尾的元素。
比如,要选择 a 节点,里面的 href 属性以 .com 结尾,就可以这样写:
1
2
|
a4 = wd.find_element(By.CSS_SELECTOR, 'a[href$=".com"]')
print(a4.get_attribute('outerHTML'))
|
如果一个元素具有多个属性。
1
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
CSS 选择器可以指定选择的元素要同时具有多个属性的限制:
1
2
|
a = wd.find_element(By.CSS_SELECTOR, 'meta[name="viewport"][content^="width"][content$="1.0"]')
print(a.get_attribute('outerHTML'))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.implicitly_wait(3)
wd.get('D:\VSCodeProjects\VSCodeP1\h4.html')
# 属性 href 值为 https://www.baidu.com
a1 = wd.find_element(By.CSS_SELECTOR, '[href="https://www.baidu.com"]')
print(a1.get_attribute('outerHTML'))
# 包含 baidu
a2 = wd.find_element(By.CSS_SELECTOR, 'a[href*="baidu"]')
print(a2.get_attribute('outerHTML'))
# 以 https 开头
a3 = wd.find_element(By.CSS_SELECTOR, 'a[href^="https"]')
print(a3.get_attribute('outerHTML'))
# 以 .com 结尾
a4 = wd.find_element(By.CSS_SELECTOR, 'a[href$=".com"]')
print(a4.get_attribute('outerHTML'))
# 同时指定多个限制
a5 = wd.find_element(By.CSS_SELECTOR, 'meta[name="viewport"][content^="width"][content$="1.0"]')
print(a5.get_attribute('outerHTML'))
wd.quit()
|
其他方法
窗口大小
1
|
webdriver.Chrome().get_window_size()
|
1
|
webdriver.Chrome().set_window_size(x, y)
|
获取当前窗口标题
浏览网页的时候,我们的窗口标题是不断变化的,可以使用WebDriver的title属性来获取当前窗口的标题栏字符串。
1
|
webdriver.Chrome().title
|
获取当前窗口 url
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(5)
# 打开网站
driver.get('https://www.163.com')
# 获取网站标题栏文本
print(driver.title)
# 获取网站地址栏文本
print(driver.current_url)
|
截屏
有的时候,我们需要把浏览器屏幕内容保存为图片文件。
比如,做自动化测试时,一个测试用例检查点发现错误,我们可以截屏为文件,以便测试结束时进行人工核查。
可以使用 WebDriver 的 get_screenshot_as_file 方法来截屏并保存为图片。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 方法1
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(5)
# 打开网站
driver.get('https://www.baidu.com/')
# 截屏保存为图片文件
driver.get_screenshot_as_file('1.png')
# 方法2
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 截图保存在本地
x = driver.get_screenshot_as_png()
# 将二进制保存成图片形式
file = open('2.png', "wb")
file.write(x)
|
练习
-
使用 selenium 登录 bysms 系统。
-
使用 selenium 查找并在控制台打印 bysms 系统中所有包含 “深圳” 的客户。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from selenium import webdriver
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()
wd.implicitly_wait(3)
wd.get('http://127.0.0.1:8080/mgr/sign.html')
wd.find_element(By.CSS_SELECTOR, 'input[type="username"][id="username"]').send_keys('byhy')
wd.find_element(By.CSS_SELECTOR, 'input[type="password"][id="password"]').send_keys('88888888')
wd.find_element(By.CSS_SELECTOR, 'button[class="btn btn-primary btn-block btn-flat"]').click()
wd.find_element(By.CSS_SELECTOR, 'input[placeholder="请输入关键词搜索"]').send_keys('深圳\n')
lists = wd.find_elements(By.CSS_SELECTOR, 'div[class="search-result-item"] > div[class="search-result-item-field"]')
print(lists)
for i in lists:
print(i.text)
wd.close()
|