2012-10-05 41 views
0

我使用硒standalon-2.25.0和Chrome的版本是13RemoteDriver使用Chrome瀏覽器下拉菜單上失敗

下面是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,但不是鍍鉻:

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工作正常。任何幫助將非常感激。再次感謝。

布萊恩

回答

1

做它的其他方式...

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

你的XPath查詢似乎並沒有一個有效的。它甚至沒有邏輯意義。沿着樹向下走,不起來。

+0

感謝艾倫就是這樣。我不知道爲什麼我決定上樹而不是下樹。其實我甚至感到驚訝它在FF和IE中工作。 –

0

除了提到的方法Arran,也嘗試使用CSS選擇器。它們比xPaths工作得更快。

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

也不要忘記用行動建設者API

來驗證ffox locator verify

方法1

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

方法2在firepath發現定位器,螢火蟲插件

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

更多信息生成器,你可以使用jsExecutor點擊網頁元素上得到here

方法3。在任何情況下總是適合我。

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

希望現在這對你的作品)

+0

謝謝,我不知道CSS選擇器比xpath工作得更快。我一定會在將來使用它。 –

相關問題