我在网上看到了大约 100 个Java webdriver 的触摸事件示例,但没有一个用于 python 的。有人会好心在这里发布一个,这样可以节省人们很多小时的搜索时间吗?这是我尝试在 android 模拟器中对一个元素进行基本的 double_tap 以放大它。非常感谢
编辑:感谢朱利安的帮助,我能够找出丢失的链接:由于某种原因,触摸动作最后需要一个额外的.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());