selenium基本操作
- 控制浏览器窗口大小
case.set_window_size(480, 800)
case.maximize_window()
- 控制浏览器前进后退
case = webdriver.Chrome()
case.get("https://www.baidu.com")
case.back()
case.forward()
- 刷新页面
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()
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)
print(a.get_attribute('class'))
print(a.is_displayed())
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()
|