自动化测试框架Selenium的使用——安装Selenium

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dongdong9223/article/details/82761255

转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/82761255
本文出自【我是干勾鱼的博客

Ingredient:

1 Selenium是什么

Selenium是一个自动化测试框架,广泛的用于自动化测试领域(是不是真的广泛用于自动化测试领域我也不知道,没怎么搞过自动化测试 -_-!,这是我臆测的 = ̄ω ̄=)。因为它能够模拟人工操作,比如能在浏览器中点击按钮、在输入框中输入文本、自动填充表单、还能进行浏览器窗口的切换、对弹出窗口进行操作。也就是说你能手动做的东西,基本都能用它来实现自动化!

2 安装Selenium

在python3下安装Selenium,使用命令:

pip3 install selenium

3 下载Chrome浏览器指定版本驱动

使用Selenium经常会在浏览器下使用,当然Chrome浏览器是较为常用的,经常Chrome中访问网页时会遇到如下错误:

selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

其实这是因为没有找到合适的Chrome驱动,解决办法就是讲chromedriver下载下来,并制定路径,既可以指定到环境变量里,也可以在selenium运行文件中指定这里采用后者。

先到chromedriver官网下载与你所安装的Chrome浏览器版本相匹配的chromedriver,这里注意,笔者Chrome浏览器版本是69,我们找到当前最新的chromedriver 2.42版本这里,打开note.txt文件,查看这个版本浏览器对应的chromedriver驱动,能够看到:

----------ChromeDriver v2.42 (2018-09-13)----------
Supports Chrome v68-70
Resolved issue 2144: Test testClickElement from MobileEmulationCapabilityTest class fails on Chromium for all platforms [[Pri-1]]
Resolved issue 2566: whitelisted-ips not working with ipv4 [[Pri-1]]
Resolved issue 2541: chromedriver v2.41 fails to start with whitelisted-ips flag on macOS [[Pri-1]]
Resolved issue 2057: Set Timeouts is not implemented [[Pri-1]]
Resolved issue 1938: Take element screenshot not implemented in Chromedriver [[Pri-2]]
Resolved issue 2550: chromedriver ignores PATH when searching for chrome binary [[Pri-2]]
Resolved issue 1993: Fullscreen Window command not spec compliant [[Pri-2]]
Resolved issue 2501: Implement log-replay functionality [[Pri-2]]
Resolved issue 2552: Some error codes are not standard compliant [[Pri-2]]
Resolved issue 669: console.log with multiple arguments not handled properly [[Pri-2]]
Resolved issue 2545: Getting “unknown error: getting size failed to return x” for SVG rect [[Pri-2]]
Resolved issue 2571: chromedriver 2.35 ~ 2.41 - touch emulation not working (swipe) [[Pri-]]
----------ChromeDriver v2.41 (2018-07-27)----------
Supports Chrome v67-69
Resolved issue 2458: Chromedriver fails to start with whitelisted-ips option [[Pri-1]]
Resolved issue 2379: Returned capabilities should include remote debugging port [[Pri-2]]
Resolved issue 1005: driver.manage().window().getSize() is not implemented on Android [[Pri-2]]
Resolved issue 2474: desktop launcher error messages are not readable by users [[Pri-]]
Resolved issue 2496: Fail fast when not able to start binary [[Pri-]]
Resolved issue 1990: Close Window return value does not conform with spec [[Pri-]]
----------ChromeDriver v2.40 (2018-06-07)----------
Supports Chrome v66-68
Resolved issue 2446: Regression: Chromedriver 2.39 hangs on open when user-data-dir is specified and exists [[Pri-1]]
Resolved issue 779: Make ChromeDriver able to listen on requests from IPv6. [[Pri-1]]
Resolved issue 2339: Chromedriver couldn’t find the Android file using valid file path [[Pri-2]]
Resolved issue 2307: /session/:sessionId/send_command and /session/:sessionId/send_command_and_get_result should be changed to be proper extension commands [[Pri-]]

可知v2.41、v2.42都支持Chrome的69这个版本,我们下载chromedriver 2.41

然后在使用selenium时指定驱动保存的路径,例如:

#指定使用的浏览器,初始化webdriver
driver = webdriver.Chrome(executable_path='/Users/yuhaidong/studying/selenium/chromedriver')

就可以了,详见下面测试中的代码。

4 测试

4.1 实例1

创建文件:

seleniumtest.py

内容如下:

from selenium import webdriver  				#导入Selenium的webdriver
from selenium.webdriver.common.keys import Keys	#导入Keys

#指定使用的浏览器,初始化webdriver
driver = webdriver.Chrome(executable_path='/Users/username/studying/selenium/chromedriver')
#请求网页地址
driver.get("http://www.python.org")
#看看Python关键字是否在网页title中,如果在则继续,如果不在,程序跳出。
assert "Python" in driver.title
#找到name为q的元素,这里是个搜索框
elem = driver.find_element_by_name("q")
#清空搜索框中的内容
elem.clear()								
#在搜索框中输入pycon
elem.send_keys("pycon")
#相当于回车键,提交
elem.send_keys(Keys.RETURN)
#如果当前页面文本中有“No results found.”则程序跳出
assert "No results found." not in driver.page_source
#关闭webdriver
driver.close()  

注意到这里面的:

driver = webdriver.Chrome(executable_path='/Users/username/studying/selenium/chromedriver')

就制定了chromedriver的存放路径。

运行:

python3 seleniumtest.py

就能够出现Chrome浏览器被打开并进行搜索然后关闭,如果觉得一闪而过可以将“driver.close() ”注释掉再运行一下查看结果。

4.2 实例2

新建文件:

seleniumtest_unsplash.py

内容如下:

from selenium import webdriver  				#导入Selenium的webdriver
from selenium.webdriver.common.keys import Keys	#导入Keys
from bs4 import BeautifulSoup					#导入BeautifulSoup模块
import time										#导入time模块

#指定使用的浏览器,初始化webdriver
driver = webdriver.Chrome(executable_path='/Users/yuhaidong/studying/selenium/chromedriver')
#请求网页地址
driver.get("https://www.unsplash.com")

# 设置下拉次数
times = 3
# 循环操作下拉过程
for i in range(times):
	print("---------开始执行第", str(i + 1),"次下拉操作---------")

	#执行JavaScript实现网页下拉倒底部
	driver.execute_script("window.scrollTo(0, document.body.scrollHeight - 2000);")

	print("第", str(i + 1), "次下拉操作执行完毕!开始等待页面加载...")

	# 等待10秒(时间可以根据自己的网速而定),页面加载出来再执行下拉操作
	time.sleep(10)

all_a = BeautifulSoup(driver.page_source, 'html.parser').find_all('img', class_='_2zEKz')

print("img标签的数量是:", len(all_a))

#关闭webdriver
driver.close()

这段代码实现了利用Selenium下拉浏览器以便展示出新图片的效果。为了能在控制台中输出图片的数量,这里面还用到了BeautifulSoup。

参考

selenium:解决 ‘chromedriver’ executable needs to be in PATH 报错 | session not created exception

Python爬虫小白入门(四)PhatomJS+Selenium第一篇

chromedriver官网

阅读更多
换一批

没有更多推荐了,返回首页