软件测试测试员的那点事Ulord社区联盟

Python UI自动化测试实战 Zero to Hero Se

2019-12-11  本文已影响0人  伊洛的小屋
1.Selenium介绍
2.Selenium的安装
(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.简单的尝试打开简书
#!/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()
(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的登陆脚本
#!/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 !

上一篇下一篇

猜你喜欢

热点阅读