当前位置:首页 > IT技术 > Web编程 > 正文

web控件的交互进阶
2021-11-30 22:50:33

web控件的交互进阶

常用的操作事件(右键点击、页面滑动、表单操作等)

https://selenium-python.readthedocs.io/api.html

 

ActionChains

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

 

 

 

 

 

 

 

https://sahitest.com/demo/clicks.htm

 

 

 

 

 

 

 

 

 测试案例二,红框代码接上上图代码:

 

 

 

用法三:将某个元素拖拽到某一个元素上

 

 

 http://sahitest.com/demo/dragDropMooTools.htm

 代码接上上图

 

 

 或者将action的部分替换成如下代码也可以:

 

 

 又或者:

 

 

 

 

 

 

 

 

 

 

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys

class Testone:
    def setup(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(5)
    def teardown(self):
        self.driver.quit()
    def test_ctrl(self):
        self.driver.get("http://sahitest.com/demo/label.htm")
        input_element1=self.driver.find_element("xpath","/html/body/label[1]/input")
        input_element2 = self.driver.find_element("xpath", "/html/body/label[2]/table/tbody/tr/td[2]/input")
        input_element1.click()
        action=ActionChains(self.driver)
        action.send_keys("username").pause(1)
        action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).pause(2)#全选
        action.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).pause(2)#复制
        action.send_keys(Keys.BACK_SPACE).perform()#1 此处因为前面执行了全选操作,所以这里执行backspace会全部删除,2 这里必须要一个perform()让关于第一个输入框的操作去执行,否则你会看到程序一直在倒腾第二个输入框
        input_element2.click()
        action.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).pause(2)#黏贴
        action.send_keys(Keys.BACK_SPACE).perform()
        sleep(3)

 

 http://sahitest.com/demo/label.htm

打开同样的网址,定位输入框e1,点击这个元素(为了将光标移到输入框,为输入做准备),向输入框中输入文字“username tom”,再把删除一个字符,pause(1)是让每一步操作都等待一秒

 实际操作中,使用find_element_by_xpath方法时提示:find_element_by_xpath    deprecated funcation(弃用函数),这时需要换成简单的定位方法,如: find_element("xpath"," ") 

具体详细解决方法见如下博客:https://blog.csdn.net/m0_54510474/article/details/121090473

 不过上述的某些操作似乎不需要借助action也能直接完成,详细见如下博客:https://www.cnblogs.com/hzcya1995/p/13309155.html

 

TouchActions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文摘自 :https://www.cnblogs.com/