python-selenium的常用方法大全
driver.find_element_by_id
driver.find_element_by_name
driver.find_element_by_class
driver.find_element_by_tag_name
driver.find_element_by_xpath
driver.find_element_by_css
driver.find_element_by_link_text
driver.find_element_by_partial_text
driver.get(url)
driver.maxmize_window()
driver.implicitly_wait(times)
driver.quit() #关闭所有窗口且安全关闭session
driver.close() #只关闭当前窗口
driver.forward()
driver.back()
driver.refresh()
driver.switch_to.frame(type)
driver.switch_to.default_content()
driver.switch_to.window(name)#切换窗口
driver.current_window_handle
driver.window_handles
driver.title
driver.current_url
driver.page_source #获取页面html源代码
driver.set_window_size(width,high)
driver.get_window_size()
driver.get_window_position()
driver.get_screenshot_as_file(filename) #截图当前窗口
driver.get_screenshot_as_base64()
driver.switch_to.alert.accept()
driver.switch_to.alert.dismiss()
driver.get_cookies()
driver.add_cookie()
driver.execute_script(js)
driver.find_element_by_xpah(findType).clear()
driver.find_element_by_xath(findType).click()
driver.find_element_by_xath(findType).send_keys()
driver.find_element_by_xath(findType).get_attribute()
driver.find_element_by_xath(findType).text
driver.find_element_by_xath(findType).tag_name #获取元素标签名
driver.find_element_by_xath(findType).is_selected() #判断返回的元素结果是否被选中
driver.find_element_by_xath(findType).submit()
driver.find_element_by_xath(findType).size #获取元素尺寸
driver.find_element_by_xath(findType).is_displaed() #判断元素是否可见
driver.find_element_by_xath(findType).send_keys(file) #针对普通input标签的上传文件可以使用此方法
鼠标事件
context_click() #鼠标右击
double_click() #鼠标双击
drag_and_drop() #拖拽---从一个地方拖到另一个地方
move_to_element() #鼠标悬停
from selenium.webdriver.common.actionchains import ActionChains
ActionChains(driver).context_click(element).perform()
键盘事件
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Firefox()
driver.find_element_by_id(id).send_keys(Keys.BACK_SPACE) #删除键
driver.find_element_by_id(id).send_keys(Keys.SPACE) #空格键
driver.find_element_by_id(id).send_keys(Keys.ENTER) #回车键
driver.find_element_by_id(id).send_keys(Keys.TAB) #制表键
driver.find_element_by_id(id).send_keys(Keys.ESCAPE) #回退,取消(ESC)
driver.find_element_by_id(id).send_keys(Keys.CONTROL,'a') #全选(Ctrl+a)
driver.find_element_by_id(id).send_keys(Keys.CONTROL,'c') #复制(Ctrl+c)
driver.find_element_by_id(id).send_keys(Keys.CONTROL,'v') #粘贴(Ctrl+v)