[英]how to swipe to right or left with selenium?
我正在嘗試向右或向左滑動,但沒有按鈕或元素可以單擊它進行滑動。 我唯一的選擇是向左或向右滑動是按住鼠標和 go 向每一側滑動。 我已經嘗試過這種方法,但它對我不起作用:
Actions action = new Actions(driver);
action.clickAndHold(homePage.HeroImage).build().perform();
//you need to release the control from the test
Thread.sleep(2000);
action.moveToElement(homePage.HeroNext).release();
感謝您的幫助:) ( HeroImage
是現在顯示的圖像, HeroNext
是我要滾動到的下一個圖像,兩者都是可見的)
我也試過這段代碼,但它也不起作用。
try {
for (int kk=0; kk<=6; kk++){
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "right");
js.executeScript("mobile: scroll", scrollObject);
}
System.out.println("Swipe Successfully");
}
catch (Exception e)
{
System.out.println("Image swipe was not successfull");
}
您是否嘗試過構建並最終執行?
Actions action = new Actions(driver);
action.clickAndHold(homePage.HeroImage);
//you need to release the control from the test
Thread.sleep(2000);
action.moveToElement(homePage.HeroNext).release();
action.build().perform();
我不相信睡眠會像你想要的那樣成為行動的一部分。 如果需要硬等待,也許可以分成 2 個動作。 您還可以使用 dragAndDrop 或 dragAndDropBy。 一個拖到目標元素,另一個拖到目標位置
對 x 和 y 坐標使用拖放:
Actions action = new Actions(driver);
action.dragAndDropBy(homePage.HeroImage, 200, 0).build().perform();
在這里,驅動程序將對 HeroImage 元素進行水平拖放。 如果要垂直拖放,請設置x=0
和y="some range"
。
希望它有效。 謝謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.