gpt4 book ai didi

java - 如何用 Selenium 向右或向左滑动?

转载 作者:行者123 更新时间:2023-12-02 09:30:43 25 4
gpt4 key购买 nike

我试图向右或向左滑动,但没有按钮或元素可以单击它来滑动。我唯一的选择是向左或向右滑动,即按住鼠标并向两侧滑动。我尝试过这个方法,但它对我不起作用:

        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();

我不相信 sleep 会成为你想要的行动的一部分。如果需要苦苦等待,也许可以分为 2 次行动。您也许还可以使用dragAndDrop 或dragAndDropBy。一个拖动到目标元素,另一个拖动到目标位置

关于java - 如何用 Selenium 向右或向左滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58012330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com