Seleninum作为自动化测试的工具,自然是提供了很多自动化操作的函数,下面列举下个人觉得比较常用的函数,更多可见官方文档:
官方API文档:http://seleniumhq.github.io/selenium/docs/api/py/api.html
1.定位元素
find_element_by_class_name:根据class定位
find_element_by_css_selector:根据css定位
find_element_by_id:根据id定位
find_element_by_link_text:根据链接的文本来定位
find_element_by_name:根据节点名定位
find_element_by_partial_link_text:根据链接的文本来定位,只要包含在整个文本中即可
find_element_by_tag_name:通过tag定位
find_element_by_xpath:使用Xpath进行定位
PS:把element改为elements会定位所有符合条件的元素,返回一个List。
比如:find_elements_by_class_name
2.鼠标动作
有时需要在页面上模拟鼠标操作,比如:单击,双击,右键,按住,拖拽等。
可以导入ActionChains类:selenium.webdriver.common.action_chains.ActionChains
使用ActionChains(driver).XXX调用对应节点的行为:
click(element):单击某个节点;
click_and_hold(element):单击某个节点并按住不放;
context_click(element):右键单击某个节点;
double_click(element):双击某个节点;
drag_and_drop(so