2013-10-30 34 views
0
driver.findElement(By.xpath("//input[@type="+"checkbox"+"]/following-sibling:://td[contains(text(),"+"template"+"]")) 

我的HTML是這樣硒的webdriver拋出錯誤,而選擇的XPath

<tr> 
<td class="tablecontent"> 
<input type="checkbox" value="59781" name="templateIds"> 
</td>`enter code here` 
<td class="tablecontent"> test11 </td> 
</tr> 

org.openqa.selenium.InvalidSelectorException: The given selector //input[@type=checkbox]/following-sibling:://td[contains(text(),template] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //input[@type=checkbox]/following-sibling:://td[contains(text(),template] because of the following error: [Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "file:///C:/Users/sanjdash/AppData/Local/Temp/anonymous3529970525380845680webdriver-profile/extensions/[email protected]/components/driver_component.js Line: 5956"] Command duration or timeout: 72 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: '2.37.0', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42', time: '2013-10-18 09:51:02'

回答

5

看起來你搞砸了引號。在XPath中使用單引號來避免這樣的問題。

// if template is the text within your XPath 
driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(), 'template']")); 

// if template is your variable, then it should be 
driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(), " + template + "']")); 

另外,請仔細閱讀錯誤,它會告訴您足夠的信息,您已經足夠了。正如你所看到的,消息中的選擇器中沒有引號。

The given selector //input[@type=checkbox]/following-sibling:://td[contains(text(),template] is either invalid or does not result in a WebElement.

+0

@ user2939258:看起來你以前從來沒有接受任何答案。只是讓你知道這個答案是否解決了你的問題,請考慮接受它。 (http://meta.stackexchange.com/a/5235/220697)否則請在這裏提供一些反饋。 –

0
"//input[@type="+"checkbox"+"]/following-sibling:://td[contains(text(),"+"template"+"]" 

產生錯誤的字符串。從蟒蛇嘗試:

"//input[@type="+"checkbox"+"]/following-sibling:://td[contains(text(),"+"template"+"]" 
'//input[@type=checkbox]/following-sibling:://td[contains(text(),template]' 

您應環繞複選框,模板,報價得到

"//input[@type="checkbox"]/following-sibling:://td[contains(text(),"template"]" 

"//input[@type='checkbox']/following-sibling:://td[contains(text(),'template']" 

使用

"//input[@type="+"'checkbox'"+"]/following-sibling:://td[contains(text(),"+"'template'"+"]" 

"//input[@type='{type}']/following-sibling:://td[contains(text(),'{text}']".format(type='checkbox',text='template') 
0

您的「包含」功能沒有右括號 並且需要正確的引用。

如果複選框和模板都是字符串,試試這個:

driver.findElement(By.xpath("//input[@type='checkbox']/following-sibling:://td[contains(text(),'template')]")) 

,如果他們是變量,試試這個:

driver.findElement(By.xpath("//input[@type='" +checkbox+"']/following-sibling:://td[contains(text(),'"+template+"')]")) 
+0

請記住,您可以在答案文本編輯器中使用花括號按鈕(或用一對'')圍繞代碼將代碼行放入容器中。這使您的答案更具可讀性。 – Catch44