Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用chrome浏览器的RemoteDriver在下拉菜单上失败_Java_Xpath_Selenium_Webdriver_Selenium Grid - Fatal编程技术网

Java 使用chrome浏览器的RemoteDriver在下拉菜单上失败

Java 使用chrome浏览器的RemoteDriver在下拉菜单上失败,java,xpath,selenium,webdriver,selenium-grid,Java,Xpath,Selenium,Webdriver,Selenium Grid,我使用的是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>

我使用的是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>
这是我得到的一个例外:

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配合得很好。任何帮助都将不胜感激。再次感谢


布莱恩

换一种方式做

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

您的XPath查询似乎无效。它甚至没有逻辑意义。沿着树往下走,而不是往上走。

除了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

方法2使用actions builder 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的更多信息,您可以获得

方法3使用jsExecutor单击web元素。在任何情况下都适用于我

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

希望这对您现在起作用)

谢谢Arran,就这样。我不知道为什么我决定爬上树而不是倒下来。事实上,我甚至对它在FF和IE中的工作感到惊讶。谢谢,我不知道CSS选择器的工作速度比xpath快。我以后一定会用这个。
WebElement mnuOptionElement;
mnuOptionElement = driver.findElement(By.cssSelector(mrCssSelector));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuOptionElement).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+msCssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());