0

当我尝试使用 webdriver(在所有浏览器中发生)选择带有撇号的答案时,我得到了这个 InvalidSelectorException。我试图使用反斜杠来转义撇号,但我认为这是因为它存储在一个变量中,它没有被转义:

org.openqa.selenium.InvalidSelectorException: The xpath expression '//select[@name='titleCode']/option[@value='I'm in college']' cannot be evaluated or does notresult in a WebElement (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 219 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: 6efb7eae-3afb-4ab1-948d-146b42d2c96c
Command duration or timeout: 701 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-32-generic', java.version: '1.6.0_31'
Driver info: driver.version: RemoteWebDriver
Session ID: 1350062508295

这是java代码。变量 question 和 answer 是来自数据库的字符串列表:

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

这是HTML:

<select id="select1" class="select" name="titleCode">
<option selected="" value="">Please select...</option>
<option value="I'm in high school">I'm in high school</option>
<option value="I'm in college">I'm in college</option>
</select>

有没有办法逃避存储在变量中的字符串的撇号或其他方式解决这个问题,或者撇号甚至是问题?任何帮助将不胜感激。谢谢!

布莱恩

4

1 回答 1

0

试试这个:

    <select id="select1" class="select" name="titleCode">
    <option selected="" value="">Please select...</option>
    <option value="I&#39;m in high school">I&#39;m in high school</option>
    <option value="I&#39;m in college">I&#39;m in college</option>
</select>

选项标签之间不应该是必需的,因为它们没有发布,但以防万一。

我希望这有效

于 2012-10-12T18:09:14.713 回答