1.用By定位元素
from selenium.webdriver.common.by import By find_element(By.ID,"kw") find_element(By.Name,"wd") find_element(By.CLASS_NAME,"s_ipt") find_element() find_element() find_element() find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")
2.控制浏览器窗口的大小
set_window_size()
from selenium import webdriver driver = webdriver.Firefox() driver.get("http://www.baidu.com") print("设置浏览器宽480、高800显示") driver.set_window_size(480,800) driver.quit()
3.控制浏览器的后退、前进
back() 和 forward()
from selenium import webdriver driver = webdriver.Firefox() first_url = 'http://www.baidu.com' print("now access %s" %(first_url)) driver.get(first_url) second_url =‘http://news.baidu.com' print("now access %s" %(second_url)) driver.get(second_url) print("back to %s "%(first_url)) driver.back() print("forward to %s"%(second_url)) driver.forward() driver.quit()
3.模拟浏览器的刷新
driver.refresh()
二、简单的元素操作
clear(): 清除文本
send_keys(*value): 模拟按键输入
click(): 单击元素
from selenium import webdriver driver = webdriver.Firefox() driver.get("http://www.126.com") driver.find_element_by_id("idInput").clear() driver.find_element_by_id("idInput").send_keys("username") driver.find_element_by_id("pwdInput").clear() driver.find_element_by_id("pwdInput").send_keys("password") driver.find_element_by_id("loginBtn").click() driver.quit()
2.WebElement接口常用方法
submit()方法用于提交表单
from selenium import webdriver driver = webdriver.Firefox() driver.get("http://www.youdao.com") driver.find_element_by_id('query').send_keys('hello') driver.find_element_by_id('query').submit() driver.quit()
size:返回元素的尺寸
text:获取元素的文本
get_attribute(name):获得属性值
is_displayed():设置该元素是否用户可见
from selenium import webdriver driver = webdriver.Firefox() driver.get("http://www.baidu.com") size = driver.find_element_by_id('kw').size print(size) text = driver.find_element_by_id('cp’).text print(text) attribute=driver.find_element_by_id("kw").get_attribute('type') print(attribute) result = driver.find_element_by_id("kw").is_displayed() print(result) driver.quit()
WebElement接口还可以参考WeBDriver API
三、鼠标事件:
封装在ActionChains类中
perform(): 执行所有ActionChains中存储的行为
context_click(): 右击
double_click(): 双击
drag_and_drop():拖动
move_to_element():鼠标悬停
1.鼠标右击键操作:
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Firefox() driver.get("http://yunpan.360.pn") right_click = driver.find_element_by_id("xx") ActionChains(driver).context_click(right_click).perform() #ActionChains(driver)调用ActionChains()类,将浏览器驱动driver作为参数传入 context_click()方法用于模拟鼠标右键操作,在调用时需要指定元素定位 perform()执行所有ActionChains中存储行为,对整个操作的提交动作
2.鼠标悬停
move_to_element()用法与context_click()相同
………………………… #定位到要悬停的元素 above = driver.find_element_by_id("id") #对定位到的元素执行悬停操作 ActionChains(driver).move_to_element(above).perform()
3.鼠标双击操作
double_click方法用于模拟鼠标双击操作
…… double_click = driver.find_element_by_id("xx") ActionChains(driver).double_click(double_click).perform()
4.鼠标拖放操作
drag_and_drop(source,target)在源元素上按住鼠标左键,然后移动到目标元素上释放
source:鼠标拖动的源元素
target:鼠标释放的目标元素
#定位元素的原位置 element = driver.find_elment_by_id("xx") #定位元素要移动到的目标位置 target = driver.find_element_by_id("xx") #执行元素的拖放操作 ActionChains(driver).drag_and_drop(element,target).perform()