【问题标题】:RemoteDriver using chrome browser fails on dropdown menu使用 chrome 浏览器的 RemoteDriver 在下拉菜单中失败
【发布时间】:2012-09-27 01:34:45
【问题描述】:

我使用的是 selenium-standalon-2.25.0,chrome 是 13 版。

这里是html:

<select name="suffix" class="select">
<option value="" selected>Please select...</option>
<option value="Ms.">Ms.</option>
<option value="Mrs.">Mrs.</option>
<option value="Mr.">Mr.</option>
</select>

这是我调用以选择其中一个选项的命令。另一个是我从数据库中获取的变量,问题是我从数据库中获取的后缀。这适用于 Firefox 和 IE,但不适用于 chrome:

driver.findElement(By.xpath("//option[@value='" + other + "' and ..[@name='" + question + "']]")).click();

这是我得到的例外:

org.openqa.selenium.InvalidSelectorException: findElement execution failed;
 Unable to locate an element with the xpath expression //option[@value='Ms.' and ..[@name='suffix']] because of the following error:
Error: INVALID_EXPRESSION_ERR: DOM XPath Exception 51 (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 52 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_25'
Driver info: driver.version: EventFiringWebDriver
Session ID: bf6368f23db4a2fe27d9b96849af1b1d
Command duration or timeout: 646 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-31-generic', java.version: '1.6.0_31'
Driver info: driver.version: RemoteWebDriver
Session ID: 134947044387

我已经为此工作了一段时间,我的猜测是与我的 findElement 语句有关。奇怪的是它适用于 FF 和 IE。任何帮助将不胜感激。再次感谢。

布莱恩

【问题讨论】:

    标签: java xpath selenium webdriver selenium-grid


    【解决方案1】:

    除了 Arran 提到的方法之外,还可以尝试使用 css 选择器。它们的工作速度比 xPath 快。

    String msCssSelector= "select[name='suffix']>option[value='Ms.']"
    String mrsCssSelector=  "select[name='suffix']>option[value='Mrs.']"
    String mrCssSelector=  "select[name='suffix']>option[value='Mr.']"
    

    也不要忘记验证在 firepath 中找到的定位器,在 ffox 中的 firebug 插件

    接近 1

    driver.findElement(By.cssSelector(msCssSelector)).click();
    

    方法 2 使用操作构建器 API

    WebElement mnuOptionElement;
    mnuOptionElement = driver.findElement(By.cssSelector(mrCssSelector));
    Actions builder = new Actions(driver);
    // Move cursor to the Main Menu Element
    builder.moveToElement(mnuOptionElement).click();
    

    更多关于 Actions builder 的信息你可以得到here

    方法 3 使用 jsExecutor 点击网页元素。在任何情况下都对我有用。

    JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x = $(\'"+msCssSelector+"\');");
            stringBuilder.append("x.click();");
            js.executeScript(stringBuilder.toString());
    

    希望现在对你有用)

    【讨论】:

    • 谢谢,我不知道 CSS 选择器比 xpath 运行得更快。我以后肯定会用这个。
    【解决方案2】:

    换个方式……

    //select[@name='suffix']/option[@value='Ms.']
    

    您的 XPath 查询似乎无效。这甚至没有逻辑意义。从树上下来,不要上树。

    【讨论】:

    • 谢谢阿兰,就是这样。我不知道为什么我决定上树而不是下树。实际上,我什至对它在 FF 和 IE 中的工作感到惊讶。
    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2016-11-28
    相关资源
    最近更新 更多