用Python的Webdriver实现触摸事件的示例?

8
我在网上看到了大约100个Java webdriver的触摸事件示例, 但没有一个是针对Python的。 请有人好心在这里发布一个,这样可以节省很多搜索时间。 以下是我尝试在Android模拟器上对元素进行基本双击以放大它的代码。非常感谢。
编辑:在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()

这里是一个原始的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());
2个回答

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()

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

顺便说一下:我使用android-server-2.21.0.apk在运行Android 4.0.4的Android手机上进行了测试。以下是您的示例代码的实质性更改:

        touchactions = TouchActions(self.remote)
        print dir(touchactions)
        touchactions.double_tap(elems)

这非常有帮助。请查看我的原始问题,以获取Android webdriver的工作代码。哇呼! - HRVHackers
旁边的问题:在webdriver __init__文件中,它已经导入了:from common.touch_actions import TouchActions。为什么我们需要在测试模块中再次导入TouchActions? - HRVHackers
谢谢这个额外的问题 :) 我从中学到了新东西。如果我们导入webdriver,就不需要导入TouchActions。它作为webdriver对象的一部分可用,例如webdriver.TouchActions(...)。但是,因为您的代码单独导入了Remote和DesiredCapabilities,所以TouchActions不在导入的命名空间中。尝试使用from selenium import webdriver,然后使用dir(webdriver.TouchActions)查看方法。FYI,请比较cat py/selenium/webdriver/remote/__init__.pycat py/selenium/webdriver/__init__.py - JulianHarty
1
如果您只使用触摸操作,为什么需要导入动作链? - Akin Hwan
1
@AkinHwan 关于 ActionChains 的导入问题,你的问题很好,我不确定是否需要它。我在6年前写了这个答案,当时的代码足以帮助提问者。我现在没有在这个领域积极工作。请随意尝试并根据测试结果更新答案。至于你的另一个问题,我不知道是否有适当的问题 - 如果找不到合适的问题,那么在StackOverflow上提出一个新问题怎么样? - JulianHarty
显示剩余2条评论

2
您可以使用移动设备仿真与selenium一起使用,从而使触摸操作正常工作。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

mobile_emulation = { "deviceName": "Nexus 5" }

chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(chrome_options = chrome_options)

然后按照通常的方式使用:

from selenium.webdriver import TouchActions

driver.get("http://example.com/")

element = context._selenium_browser.find_element_by_link_text("More information...")

touch_actions = TouchActions(driver)
touch_actions.tap(element).perform()

driver.current_url  # yields: https://www.iana.org/domains/reserved

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接