使用webdriver python的触摸事件示例?

gor*_*sbm 6 python selenium android webdriver

我在网上看到了大约100个针对Java Webdriver的触摸事件示例,但没有一个针对python。有人愿意在这里发布一个,这样可以为人们节省很多搜索时间吗?这是我尝试在android模拟器中的元素上进行基本double_tap以便放大的功能。非常感谢

编辑:由于朱利安(Julian)的帮助,我得以找出缺失的链接:由于某些原因,触摸操作最后需要一个额外的.perform()。在下面,您会发现大量的触摸事件在起作用-代码更清晰。请享用!

import unittest, time
from selenium import webdriver

print "Here are our available touch actions (ignore the ones that look like __xx__): ", dir(webdriver.TouchActions)
#print dir(webdriver)



class Test(unittest.TestCase):


    def setUp(self):
        self.driver = webdriver.Remote(command_executor='http://localhost:8080/wd/hub',  desired_capabilities=webdriver.DesiredCapabilities.ANDROID)
        self.touch =webdriver.TouchActions(self.driver)

        #self.driver = TouchActions(self.driver)
        #self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)


    def testHotmail(self):
        self.driver.get("http://www.hotmail.com")

        elem=self.driver.find_element_by_css_selector("input[name='login']")
        #tap command
        self.touch.tap(elem).perform()
        time.sleep(2)
        elem.send_keys("hello world")
        time.sleep(2)
        #double tap
        self.touch.double_tap(elem).perform()
        time.sleep(2)

        #testing that regular webdriver commands still work
        print self.driver.find_element_by_partial_link_text("Can't access").text

        elem= self.driver.find_element_by_css_selector("input[type='submit']")
        self.touch.tap(elem).perform()
        time.sleep(3)




    def tearDown(self):

        time.sleep(3)

        try:
            self.driver.quit()
        except Exception:
            print(" TearDown Method: Browser seems already closed.")

        pass


if __name__ == "__main__":
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

这是原始的Java示例:

WebElement toFlick = driver.findElement(By.id("image"));
// 400 pixels left at normal speed
Action flick = getBuilder(driver).flick(toFlick, 0, -400, FlickAction.SPEED_NORMAL)
        .build();
flick.perform();
WebElement secondImage = driver.findElement(“secondImage”);
assertTrue(secondImage.isDisplayed());
Run Code Online (Sandbox Code Playgroud)

Jul*_*rty 5

我对您的示例进行了一些调整,至少测试运行没有错误。我不知道当用户在用户名字段中双击时,您希望网站做什么?

这是修改后的代码:

import unittest, time

from selenium.webdriver import Remote
from selenium.webdriver import  DesiredCapabilities
from selenium.webdriver.remote import webelement , command
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions




class Test(unittest.TestCase):


    def setUp(self):
        remote = Remote(command_executor='http://localhost:8080/wd/hub',  desired_capabilities=DesiredCapabilities.ANDROID)
        self.remote=remote
        remote.implicitly_wait(30)

    def tearDown(self):
        pass


    def testName(self):
        # self.remote.get("http://icd.intraxinc.com/pxr")
        self.remote.get("https://icd.intraxinc.com/pxr/ext/login.action")
        elems= self.remote.find_element_by_css_selector("#j_username")
        print dir(self)
        print dir(self.remote)
        touchactions = TouchActions(self.remote)
        print dir(touchactions)
        touchactions.double_tap(elems)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

我在示例中留下了各种调试打印语句,以向您展示如何调查所面临的问题。我还将URL更改为您的登录页面重定向到的URL。这是针对我在设备上安装的某个版本的Android驱动程序的不相关问题的解决方法。

仅供参考:我在运行Android 4.0.4的Android手机上使用android-server-2.21.0.apk进行了测试。这是对示例代码的重大更改

        touchactions = TouchActions(self.remote)
        print dir(touchactions)
        touchactions.double_tap(elems)
Run Code Online (Sandbox Code Playgroud)