官方文档:
https://selenium-python.readthedocs.io/api.html
from time import sleep import pytest from selenium import webdriver from selenium.webdriver import ActionChains class TestActions(): def setup(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(3) def teardown(self): self.driver.quit() def test_case_click(self): self.driver.get("http://sahitest.com/demo/clicks.htm") click_me = self.driver.find_element_by_xpath("//input[@value='click me']") dbl_click_me = self.driver.find_element_by_xpath("//input[@value='dbl click me']") right_click_me = self.driver.find_element_by_xpath("//input[@value='right click me']") action = ActionChains(self.driver) action.click(click_me) sleep(3) action.context_click(right_click_me) sleep(3) action.double_click(dbl_click_me) sleep(3) action.perform() if __name__ == '__main__': pytest.main('-v','-s','test_click.py')
from time import sleep import pytest from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys class TestCase(): def setup(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(5) self.driver.maximize_window() def teardown(self): self.driver.quit() @pytest.mark.skip def test_case_clike(self): self.driver.get("http://sahitest.com/demo/clicks.htm") ele_click = self.driver.find_element_by_xpath("//input[@value='click me']") ele_doubleclick = self.driver.find_element_by_xpath("//input[@value='dbl click me']") ele_rightclick = self.driver.find_element_by_xpath("//input[@value='right click me']") action = ActionChains(self.driver) action.click(ele_click) action.context_click(ele_rightclick) action.double_click(ele_doubleclick) sleep(3) action.perform() sleep(3) @pytest.mark.skip def test_moveto(self): self.driver.get('https://www.baidu.com/') ele = self.driver.find_element_by_link_text("设置") action = ActionChains(self.driver) action.move_to_element(ele) action.perform() sleep(5) def test_keys(self): self.driver.get("https://www.baidu.com/") ele = self.driver.find_element_by_id("kw").click() action = ActionChains(self.driver) action.send_keys("uesername").pause(2) action.send_keys(Keys.SPACE).pause(2) action.send_keys("tom").pause(2) action.send_keys(Keys.BACK_SPACE).perform() sleep(3)
from time import sleep from selenium import webdriver from selenium.webdriver import TouchActions class TestTouch(): def setup(self): option = webdriver.ChromeOptions() option.add_experimental_option('w3c',False) self.driver = webdriver.Chrome(options= option) self.driver.implicitly_wait(5) def teardown(self): self.driver.quit() def test_touchaction(self): self.driver.get("http://www.baidu.com") el = self.driver.find_element_by_id("kw") el_search = self.driver.find_element_by_id("su") el.send_keys("selenium测试") action = TouchActions(self.driver) action.tap(el_search) action.perform() sleep(3) action.scroll_from_element(el,0,10000).perform()
表单操作