webElement 中常见的元素操作

突然发现,其实我们对元素的操作,其实很少,无非就是鼠标点击,输入文本。

清除文本输入框

clear(self)

点击元素

click(self)

提交表单

submit(self)

发送信息

send_keys(self, *value)

WebElement 中常见的元素属性

获取元素属性

get_attribute(self, name)

判断元素可见

is_displayed(self)

判断元素可用

is_enabled(self)

判断元素是否被选中

is_selected(self)

WebElement 中常见的属性方法

获取元素位置

location

获取元素大小

size

获取元素的文本

text

ActionChains 中的复杂操作

ActionChains中提供一些复杂操作,如鼠标移动,鼠标按钮操作,按键操作和上下文菜单交互等,这些操作在实际运用中其实复杂操作并不常用,这里只简单介绍一个元素拖拽的例子,其他方法,请自行查看源码。

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)

拖拽(在元素A上按住鼠标左键不放,拖动到元素B上,然后松开鼠标)

action_chains.drag_and_drop(element, target).perform()

转载至 https://www.kancloud.cn/guanfuchang/python_selenium