Selenium Actions DragAndDrop意外导航到Google搜索页面 [英] Selenium Actions dragAndDrop unexpectedly navigates to google search page

查看:149
本文介绍了Selenium Actions DragAndDrop意外导航到Google搜索页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在下一页 https://www.seleniumeasy.com/test/drag-and-drop-demo.html

I've been trying to practice with drag and drop tests on following page https://www.seleniumeasy.com/test/drag-and-drop-demo.html

这是只有一种方法的PageObject类的代码,该方法将列表的元素拖放到dropzone:

Here is the code of PageObject class with only one method, which drags and drops element of the list to the dropzone:

public class DragAndDropDemoPage extends Page {

    public DragAndDropDemoPage(WebDriver driver) {
        super(driver);
        navigateTo();
    }

    @Override
    public void navigateTo() {
        driver.get(Urls.DRAG_AND_DROP_PAGE_URL);
    }

    public void dragAndDropElementWithIndex(int index) {
        List<WebElement> elements = findElementsByCss("span[draggable=true]");
        new Actions(driver).dragAndDrop(elements.get(index), findElementByCss("div#mydropzone")).build().perform();
    }
}

执行此类操作后,WebDriver最终将导航至Google搜索页面.搜索请求包含我一直拖动的元素的名称.

after performing of such an action WebDriver eventually navigates to the Google Search page. And search request contains the name of the element I've been dragging.

这种情况怎么了? WebDriver如何设法移动到另一个页面?

What's wrong with this case? How did WebDriver managed to move to another page?

PS:超级类页面是:

PS: Super class Page is:

public abstract class Page {
    WebDriver driver;

    public Page(WebDriver driver) {
        this.driver = driver;
    }

    public abstract void navigateTo();

    protected WebElement findElementByCss(String cssSelector) {
        return driver.findElement(By.cssSelector(cssSelector));
    }

    protected List<WebElement> findElementsByCss(String cssSelector) {
        return driver.findElements(By.cssSelector(cssSelector));
    }

    protected WebElement findByXpath(String xpath) {
        return driver.findElement(By.xpath(xpath));
    }
}

推荐答案

问题:源元素位于鼠标位置而不是目标元素.在其他网站上进行拖放操作,可以在此处进行检查.
当您运行脚本时,您的鼠标位置可能位于浏览器选项卡栏附近,这就是Google打开的原因(与您将任何文本拖到浏览器栏一样).

Problem: source element is dropping to the mouse position and not to target element. Drag and drop works in other website, you can check here.
When you run script probably your mouse position is somewhere near tab bar of the browser, that's why google opens (same as you put drag any text to the browser bar).

您可以尝试@DebanjanB解决方案,也可以使用机器人.

You can try @DebanjanB solution or you can use Robot.

这篇关于Selenium Actions DragAndDrop意外导航到Google搜索页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆