我已經看到約100 touch event examples for the Java webdriver在線,但沒有一個用於python。 有人會在這裏發佈一個,所以它可以節省很多小時的搜索? 這裏是我嘗試在Android模擬器中對元素進行基本的double_tap以放大它。非常感謝使用webdriver python觸摸事件的示例?
編輯:感謝朱利安的幫助,我能夠找出缺失的鏈接:由於某種原因,觸摸操作最後需要額外的.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());
這非常有幫助。請參閱我的原始問題以瞭解Android Webdriver的工作代碼。哇噢! – gorbysbm 2013-04-12 09:22:10
方面的問題:在webdriver __init__文件中,它已經導入:從common.touch_actions導入TouchActions。爲什麼我們需要在測試模塊中再次導入觸摸操作? – gorbysbm 2013-04-12 09:35:06
感謝這個額外的問題:)我從中學到了一些新東西。如果我們導入webdriver,我們*不需要導入TouchActions。它可作爲webdriver對象的一部分,例如'webdriver.TouchActions(...)'然而,因爲你的代碼分別導入了Remote和DesiredCapabilities,看起來TouchActions不在導入的命名空間中。 '從selenium導入webdriver'然後嘗試'dir(webdriver.TouchActions)'來查看方法。 FYI比較'cat py/selenium/webdriver/remote/__ init __。py'與'cat py/selenium/webdriver/__ init __。py' – JulianHarty 2013-04-13 15:42:10