当我尝试使用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代码。变量quesiton和answer是来自DB的字符串列表:
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>
有没有办法逃避存储在变量或其他方式的字符串的撇号,或者是撇号甚至问题?任何帮助都会非常感激。谢谢!
布赖恩
答案 0 :(得分:0)
尝试一下:
<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>
选项标签之间不应该是必需的,因为它们没有发布,只是以防万一。
我希望这有效