selenium最常用的方法,适用于走流程测试

#查找元素
driver.find_element_by_xpath() #查找元素这个其实是用的最多的

#然后就是点击事件
#单击
dj=driver.find_element_by_xpath()
dj.click()
#其实也可以这么写,但是我 发现有的时候不生效,只能向上面那样子写
driver.find_element_by_xpath().click()

#双击,双击和单击的方法是不同的,没有doubleclick()方法
button = self.driver.find_element_by_xpath(button_xpath)
ActionChains(self.driver).double_click(button).perform()

#输入内容
input=driver.find_element_by_xpath()
input.send_keys("") #里面需要填写字符串,假如是数字类型就得用 str()方法转型
input.send_keys(str(20))

#睡眠
很多时候因为数据量大,或者其它原因,加载比较慢,这时候就需要用到睡眠
time.sleep(2) #1是睡眠时间,单位秒

#异常
try:
    ....
except Exception as e:
    ....

#for循环 if while循环
for i in range(1,5):
    #会输出1,2,3,4 从一开始,不写1默认为0开始

if 条件:

while 条件:
   break,continue
#与break和continue结合使用最佳,可以循环遍历,这个有的时候是for循环做不到的,必须要用while循环才行
#这个得自己去体会,遇到循环问题可以想一下怎么搞

#然后写了个登录的方法
def login(self):
        name_input = self.driver.find_element_by_xpath('路径')
        name_input.send_keys("账号")
        pwd_input = self.driver.find_element_by_xpath('路径')
        pwd_input.send_keys("密码")
        vcode_input = self.driver.find_element_by_xpath('路径')
        # input("请输入验证码后按回车:")
        vcode_input.send_keys(input("请输入验证码后按回车:"))
        submit_button = self.driver.find_element_by_xpath('路径')
        submit_button.click()
        time.sleep(6)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值