1 元素定位参数化 find_element
定位方法:
by_id="id"
by_xpath="xpath"
by_link_text="link text"
by_partial_text="partial link text"
by_name="name"
by_tag_name="tag name"
by_class_name="class name"
by_css_selector="css selector"
#coding:utf-8
from selenium import webdriver
#from selenium.webdriver.common.by import By
driver=webdriver.Firefox()
driver.get("https://www.baidu.com")
driver.find_element("id","kw").send_keys('python')
driver.find_element("css selector","#su").click()
2 登录 参数化
把登录封装为一个方法,然后把账号和密码参数化。登录的时候只需要调用这个方法
1把输入账号、输入密码、点击登录按钮三个步骤写成一个方法
2然后把输入的账号和密码参数化
#coding:utf-8
from selenium import webdriver
import unittest
import time
class Bolg(unittest.TestCase):
u'''登录博客'''
def setUp(self):
self.driver=webdriver.Firefox()
url="https://account.cnblogs.com/signin"
self.driver.get(url)
self.driver.implicitly_wait(30)
def login(self,username,psw):
u'''这里写了一个登录的方法,账号和密码参数化'''
self.driver.find_element_by_id("mat-hint-0").send_keys(username)
self.driver.find_element_by_id("mat-input-1").send_keys(psw)
self.driver.find_elements_by_css_selector("action-button")[0].click()
time.sleep(3)
判断是否登录成功,取登录成功后账户名 -封装判断方法:获取到账户名返回True,没有获取到返回False
#coding:utf-8
from selenium import webdriver
import unittest
import time
class Bolg(unittest.TestCase):
u'''登录博客'''
def setUp(self):
self.driver=webdriver.Firefox()
url="https://account.cnblogs.com/signin"
self.driver.get(url)
self.driver.implicitly_wait(30)
def login(self,username,psw):
u'''这里写了一个登录的方法,账号和密码参数化'''
self.driver.find_element_by_id("mat-input-0").send_keys(username)
self.driver.find_element_by_id("mat-input-1").send_keys(psw)
self.find_elements_by_css_selector("button.action-button")[0].click()
time.sleep(3)
def is_login_sucess(self):
u'''判断是否获取到登录账户名称'''
try:
text=self.find.element_by_id("lnk_current_user").text
print(text)
return True
except:
return False
def test_01(self):
u'''登录案例参考:账号、密码自己设置'''
self.login('','')
result=self.is_login_sucess()
self.assertTrue(result)
def test_02(self):
u'''登录案例参考:账号,密码自己设置'''
self.login("","")
result=self.is_login_sucess()
self.assertTrue(result)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main()