Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用webdriver python的触摸事件示例?_Android_Python_Selenium_Webdriver - Fatal编程技术网

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

Android 使用webdriver python的触摸事件示例?,android,python,selenium,webdriver,Android,Python,Selenium,Webdriver,我在网上看到了大约100个,但没有一个是关于python的。 有人会不会在这里贴一个,这样可以节省人们很多时间的搜索? 下面是我尝试在android模拟器中对一个元素进行基本的双击,以便放大它。非常感谢 编辑:多亏了朱利安的帮助,我才找到了丢失的链接:出于某种原因,触摸动作需要在最后添加一个.perform()。在下面,您将看到一组正在运行的触摸事件——代码更清晰。享受吧 import unittest, time from selenium import webdriver print "H

我在网上看到了大约100个,但没有一个是关于python的。 有人会不会在这里贴一个,这样可以节省人们很多时间的搜索? 下面是我尝试在android模拟器中对一个元素进行基本的双击,以便放大它。非常感谢

编辑:多亏了朱利安的帮助,我才找到了丢失的链接:出于某种原因,触摸动作需要在最后添加一个.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());

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

修订后的守则如下:

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驱动程序版本中遇到的一个无关问题的方法

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

        touchactions = TouchActions(self.remote)
        print dir(touchactions)
        touchactions.double_tap(elems)
您还可以使用使触摸动作与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

这很有帮助。关于Android webdriver的工作代码,请参阅我的原始问题。呜呼!附带问题:在webdriver init文件中,它已经导入:from common.touch\u actions import touch actions。为什么我们需要在测试模块中再次导入触摸动作?谢谢你提出这个额外的问题:)我从中学到了一些新东西。如果我们导入webdriver,则不需要导入TouchActions。它是webdriver对象的一部分,例如,
webdriver.TouchActions(…)
但是,由于您的代码分别导入远程功能和所需功能,因此TouchActions似乎不在导入的命名空间中
从selenium导入webdriver
然后尝试
dir(webdriver.TouchActions)
查看方法。仅供参考,将
cat py/selenium/webdriver/remote/\uuuu init\uuuuu.py
cat py/selenium/webdriver/\uuuu init\uuuuuuuu.py
进行比较,如果您只使用触摸动作,为什么需要导入动作链?@AkinHwan关于导入
动作链的问题很好,我不知道是否需要,我6年前写了这个答案,代码足够好,可以帮助提出这个问题的人。我没有在这个领域积极工作。您可以根据测试结果自由试验和更新答案。至于你的另一个问题,我不知道是否有合适的问题——如果你找不到,那么就再问一个关于StackOverflow的新问题怎么样?