selenium webdriver (3)

原文链接: http://www.cnblogs.com/qihui/p/4221636.html

鼠标事件:

ActionChains 类
 context_click() 右击
 double_click() 双击
 drag_and_drop() 拖动
鼠标右键:

from selenium.webdriver.common.action_chains import ActionChains #导入ActionChains包

#
定位到要右击的元素 qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table /tbody/tr/td[2]") #对定位到的元素执行鼠标右键操作 ActionChains(driver).context_click(qqq).perform() ''' #你也可以使用三行的写法,但我觉得上面两行写法更容易理解 chain = ActionChains(driver) implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/ tbody/tr/td[2]") chain.context_click(implement).perform() '''

鼠标的其他操作:

#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

定位一组元素:

from selenium import webdriver
import time
import os
dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('checkbox.html' )
dr.get(file_path)
# 选择页面上所有的 input,然后从中过滤出所有的 checkbox 并勾选之
inputs = dr.find_elements_by_tag_name('input' )
for input in inputs:
if input.get_attribute('type' ) == 'checkbox' :
input.click()
time.sleep(2)
dr.quit()

 方法二:

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import os
dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('checkbox.html' )
dr.get(file_path)
# 选择所有的 checkbox 并全部勾上
checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]' )
for checkbox in checkboxes:
checkbox.click()
time.sleep(2)
# 打印当前页面上有多少个 checkbox
print len(dr.find_elements_by_css_selector('input[type=checkbox]' ))
time.sleep(2)
dr.quit()
# 把页面上最后1个 checkbox 的勾给去掉
dr.find_elements_by_css_selector('input[type=checkbox]' ).pop().cl
ick()
time.sleep(2)

 框架或窗口定位:

browser.switch_to_frame("f1")
#再找到其下面的 ifrome2(id =f2)
browser.switch_to_frame("f2")
#下面就可以正常的操作元素了
driver.switch_to_window("windowName")

转载于:https://www.cnblogs.com/qihui/p/4221636.html

猜你喜欢

转载自blog.csdn.net/weixin_30947043/article/details/94789559