一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210408
【Selenium核心技术篇】包含以下内容:
- Selenium实现元素定位(8种)
- Selenium WebDriver
- Selenium WebElement
- Selenium操作from表单
- Selenium操作checkbox和radiobutton
- Selenium操作下拉列表
- Selenium处理弹框
- Selenium三种等待方式
- Selenium等待条件
- Selenium鼠标和键盘事件
- Selenium执行JavaScript脚本
- Selenium屏幕截图
- Selenium之frame的切入和切出
# 通过CSS定位搜索框,并输入selenium
driver.find_element_by_css_selector('#kw').send_keys('selenium')
from selenium import webdriver
from time import sleep
# --原型--------------------------------
# driver = webdriver.Chrome()
# driver.get('http://wwww.baidu.com')
# driver.maximize_window()
# sleep(1)
#
# element = driver.find_element_by_id('kw')
# element.send_keys('留白')
# print(type(element))
#
# driver.find_element_by_id('su').click()
# sleep(3)
# driver.close()
#----------------------------------------
# 封装如下
class TestCase(object):
def __init__(self):
#self全局
self.driver = webdriver.Chrome()
self.driver.get('http://wwww.baidu.com')
self.driver.maximize_window()
sleep(1)
def test_id(self):
element = self.driver.find_element_by_id('kw')
element.send_keys('留白')
print(type(element))
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_name(self):
element = self.driver.find_element_by_name('wd')
element.send_keys('留白')
print(type(element))
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_linktext(self):
self.test_id()
self.driver.find_element_by_link_text('百度首页').click()
sleep(3)
self.driver.quit()
def test_partial_link_text(self):
self.test_id()
self.driver.find_element_by_partial_link_text('百度首页').click()
sleep(3)
self.driver.quit()
def test_xpath(self):
self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys('留白')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
# 一般不用
def test_tag(self):
input = self.driver.find_element_by_tag_name('input')[0]
print(input)
self.driver.quit()
def test_css_selector(self):
self.driver.find_element_by_css_selector('#kw').send_keys('留白')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_class_name(self):
self.driver.find_element_by_class_name('s_ipt').send_keys('留白')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
def test_all(self):
self.driver.find_element(value='kw').send_keys('留白')
self.driver.find_element_by_id('su').click()
sleep(3)
self.driver.quit()
if __name__ == '__main__':
case = TestCase()
# case.test_id()
# case.test_name()
# case.test_linktext()
# case.test_partial_link_text()
# case.test_xpath()
# case.test_tag()
# case.test_css_selector()
# case.test_class_name()
case.test_all()
第一次封装,代码如下:
报错:
def __int__(self):
漏了一个i,应为
def __init__(self):
我找个半个多小时也不知道哪里错了,e mmm,被自己蠢哭