享受每一天,Just Enjoy !

0%

Python UI自动化测试实战 Zero to Hero Selenium基础(一)

1.Selenium介绍
2.Selenium的安装
1
2
3
4
5
6
(yiluo) ➜  ~ pip install selenium
Collecting selenium
Using cached https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl
Requirement already satisfied: urllib3 in ./yiluo/lib/python3.7/site-packages (from selenium) (1.25.7)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
3.安装谷歌浏览器驱动
4.简单的尝试打开简书
1
2
3
4
5
6
7
8
9
#!/usr/bin/python3

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.jianshu.com/u/3c9531339206')
time.sleep(3)
driver.close()
1
(yiluo) ➜  Code python web.py

自动打开的网页

6.定位元素的一些方式

id
name
class name
tag name
link text
partial link text
xpath
css selector
######7. Python webdriver 中的使用方法

find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()

8.实例软件测试网站Testerhome的登陆脚本
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
#!/usr/bin/python3

from selenium import webdriver
from time import sleep


driver = webdriver.Chrome()

driver.get("https://testerhome.com/")
# 放大窗口
driver.maximize_window()
sleep(3)

# 进入登陆页面
driver.find_element_by_xpath('//*[@id="main-page"]/div[1]/nav/div/ul[1]/li[2]/a').click()

sleep(3)

# 输入账号
driver.find_element_by_id('user_login').send_keys('你的账户')
# 输入密码
driver.find_element_by_id('user_password').send_keys('你的密码')
# 点击登陆按钮
driver.find_element_by_xpath('//*[@id="new_user"]/div[4]/input').click()

sleep(3)
# 关闭窗口
driver.close()

登陆成功

……待续


在这里插入图片描述

享受每一天,Just Enjoy !

关注公众号获取更多内容