Example of touch event with webdriver python?
android, python, selenium, webdriver
Solution
I've made some tweaks to your example, at least the test runs without error. I don't know what you expect the web site to do when a user double-taps in the username field...
Here is the revised code:
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()
I left various debug print statements in the example to show you how I investigated the problem you were facing. I also changed the URL to the one your login page redirects to. This was a workaround for an unrelated problem I had with a version of the Android driver, installed on the device.
FYI: I tested with android-server-2.21.0.apk on an Android phone running 4.0.4 of Android. Here are the material changes to your example code
touchactions = TouchActions(self.remote)
print dir(touchactions)
touchactions.double_tap(elems)
Problem
I have seen about 100 touch event examples for the Java webdriver online, but not a single one for python. Would someone be so kind as to post one here, so it saves people many hours of search? Here is my attempt to do a basic double_tap on an element in an android simulator in order to zoom in on it. Much thanks EDIT: Thanks to Julian's help I was able to figure out the missing link: for some reason, the touch actions require an extra .perform() at the end. Below you will find a bunch of touch events in action--and the code is cleaner. Enjoy! ``` 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() ``` Here is an original Java Example: ``` 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()); ```