- 浏览器操作
- driver.maximize_window() #浏览器最大化
- driver.set_window_size(480,800) #设置浏览器宽,高
- driver.back() & driver.forward() #后退,前进
- 定位
- find_element_by_id()
- find_element_by_name()
- find_element_by_class_name()
- find_element_by_tag_name()
- find_element_by_link_text()
- find_element_by_partial_link_text()
- find_element_by_xpath()
- find_element_by_css_selector()
- 操作测试对象
- clear()
- send_keys()
- click()
- submit() #提交表单
WebElement接口常用方法
- driver.find_element_by_id("kw").size #返回元素的尺寸
- driver.find_element_by_id("kw").text #返回元素的文本
- driver.find_element_by_id(“kw").get_attribute('type') #获取元素属性值
- result=driver.find_element_by_id(“kw").is_displayed() #元素是否可见
- context_click() #右击
- double_click()
- drag_and_drop()
- move_to_element() #鼠标悬停在一个元素之上
- click_and_hold() #按下鼠标左键在一个元素之上
实例:
from selenium.webdriver.common.action_chains import ActionChains
right=drvier.find_element_by_xpath("xxx")
ActionChains(driver).context_click(right).perform()
- 键盘事件
- send_keys(Keys.BACK_SPACE)
- send_keys(Keys.SPACE)
- send_keys(Keys.TAB
- send_keys(Keys.ESCAPE) #回退键ESC
- send_keys(Keys.ENTER)
- send_keys(Keys.CONTROL,'a')
- send_keys(Keys.CONTROL,'c')
- send_keys(Keys.CONTROL,'x')
- send_keys(Keys.CONTROL,'v')
from selenium.webdriver.common.keys import Keys
- 打印信息
- driver.title
- driver.current_url
- driver.find_element_by_id("xxx").text