【问题标题】:Getting Error while running moveToElement() method in selenium webdriver? [duplicate]在 selenium webdriver 中运行 moveToElement() 方法时出错? [复制]
【发布时间】:2017-07-20 17:56:25
【问题描述】:

我对 selenium 还是很陌生,当我在 Eclipse (Java) 中为 Selenium WebDriver 运行下面的代码时,我遇到了这个异常:

“线程“主”中的异常 org.openqa.selenium.UnsupportedCommandException: mouseMoveTo"

代码如下:

    WebDriver driver = new FirefoxDriver();
    driver.get("http://newtours.demoaut.com/");
    WebElement myElement = driver.findElement(By.cssSelector("input[name=userName]"));

    Actions myAction = new Actions(driver);
    myAction.moveToElement(myElement)
        .click()
        .keyDown(myElement, keys.SHIFT)
        .sendKeys(myElement, "test")
        .keysUp(myElement, keys.SHIFT)
        .(myElement)
        .contextClick()
        .build()
        .perform();

是什么导致了这个错误?

感谢和问候。

【问题讨论】:

    标签: java eclipse selenium selenium-webdriver


    【解决方案1】:

    新版本的 FirefoxDriver 存在一个已知问题,即 GeckoDriver,它不支持 Action 类,请参阅:

    Selenium web driver moveToElement (Actions) throwing error with marionette driver?

    https://github.com/SeleniumHQ/selenium/issues/3348

    如果没有更多信息,我认为这也是您的问题。如果您需要使用 FF 进行测试,请使用旧版本或带有 ChromeDriver 的 Chrome

    【讨论】:

    • 谢谢老兄.. 我使用了 ChromeDriver() 并且它有效!!
    【解决方案2】:

    在 xpath 下试试这个:

    解释:你的input标签,父标签是table所以以table标签开始xpath,然后使用following关键字继续input标签。

    driver.findElement(By.xpath("//table/../following::input[@name='userName']")).sendKeys("USERNAME");
    

    【讨论】:

    • 嗨,我的代码在操作类初始化之前一直在工作。但是谢谢回复
    • 正如 Enrique 指出的,我认为问题一定出在 GreckoDriver ..
    • @Rishabh 你能解释一下为什么要在这里使用Action class 来处理这个username 文本框吗?
    • 这不是正确的练习方法,你的方法在这里完全错误。 Action类用于拖拽元素和元素的悬停效果来使用。
    • 好的.. 感谢您的指出。会记住的..
    【解决方案3】:

    说明:您的cssSelector 不正确,属性值使用单引号。

    driver.findElement(By.cssSelector("input[name='userName']"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2018-01-25
      • 1970-01-01
      • 2017-06-03
      • 2013-12-23
      • 2015-03-24
      相关资源
      最近更新 更多