selenium常见方法

selenium基本操作

  1. 控制浏览器窗口大小
case.set_window_size(480, 800) #设置像素大小
case.maximize_window()	#最大化
  1. 控制浏览器前进后退
case = webdriver.Chrome()
case.get("https://www.baidu.com")
case.back()       #后退
case.forward()    #前进           

  1. 刷新页面
case.refresh()

4.常用方法

用途方法
清除文本clear()
输入文本send_keys(value)
点击click()
提交表单submit()
获取元素文本text
获取元素尺寸size
获取元素属性get_attribute(name)
用户是否可见该元素is_displayed()
  • 例子
 		case = webdriver.Chrome()
    case.get("http://www.baidu.com/")
    case.find_element(By.ID, 'kw').send_keys('selenium')
    time.sleep(5)
    case.find_element(By.ID, 'kw').clear()
    # case.find_element(By.ID,'su').click()
    # case.find_element(By.ID, 'kw').send_keys('selenium').submit()
    time.sleep(3)
    case.quit()
	  case = webdriver.Chrome()
    case.get("http://www.baidu.com/")
    a = case.find_element(By.CSS_SELECTOR,
                          'div#head>div#s-top-left>a:nth-child(2)')

    print(a.text) #hao123
    print(a.get_attribute('class')) #mnav c-font-normal c-color-t
    print(a.is_displayed()) #True
    print(type(a))
    b = case.find_element(By.ID, 'kw').size
    print(b)
    time.sleep(3)
    case.quit()

5.鼠标事件

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains


def main():
    """TEST_MAIN

    """
    case = webdriver.Chrome()
    case.get("http://www.mi.com/")
    a = case.find_element(
        By.XPATH,
        '/html/body/div[1]/div[1]/div/div[3]/div[1]/div[2]/ul/li[2]/a/span')
    ActionChains(case).move_to_element(a).perform()
    time.sleep(0.5)
    case.find_element(
        By.XPATH,
        '/html/body/div[1]/div[1]/div/div[3]/div[2]/div/ul/li[1]/a/div[1]/img'
    ).click()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值