页面元素属性,action chains,下拉框

页面元素的属性

tag_name 标签名
print(ele.tag_name)
text 文本内容
parent 获取父级标签

 

 

页面元素的方法

get_attribute(属性名) 获取标签的属性
is_displayed() 判断元素是否可见
send_keys() 输入内容
click() 点击元素
clear() 清空表单输入框

 

 

action chains

鼠标操作相关

from selenium.webdriver import ActionChains
# 第一步,创建一个鼠标操作的对象
action = ActionChains(driver);
# 第二部,进行点击操作 (事实上不会进行操作,只是添加一个点击的动作
action.click(btn)  # 鼠标单击左键
# 第三步,执行操作
action.perform()

click 鼠标左击
double_click 鼠标双击
context_click 鼠标右击

将鼠标移动到某个元素上

action = ActionChains(driver)  # 创建对象 
# 第二部,添加移动
action.move_to_element(locator)
# 第三步,执行动作
action.perform()

按住并滑动

action = ActionChains(driver)
# 第一步:在滑块处按住鼠标左键
action.click_and_hold(sli_ele)
# 第二步:相对鼠标当前位置进行移动
action.move_by_offset(100,0)
# 第三步:释放鼠标
action.release()
# 执行动作
action.perform()

拖拽鼠标到目标位置

action = ActionChains(driver)
# 第一步:拖动元素
action.drag_and_drop(s, t)
# 执行动作
action.perform()
# s 开始的地方, t 目标拖到哪里

tips:

显示等待中针对鼠标点击的等待。

WebDriverWait(driver,5,0.2).until(
    EC.element_to_be_clickable((By.XPATH,"//a[text()='高级搜索']"))
).click()

下拉框的选择

导包

from selenium.webdriver.support.select import Select

首先要先定位到下拉框的元素

select_ele = driver.find_element_by_xpath("//select[@name='gpc']")
# 使用Select方法生select对象,调用对象的方法来操作
select = Select(select_ele)

通过索引选择

select.select_by_index(3)

通过文本

select.select_by_visible_text('最近一年')   

通过value

s2 = Select(driver.find_element_by_xpath("//select[@name='ft']"))
s2.select_by_value('doc')
原文地址:https://www.cnblogs.com/addicated/p/13194360.html