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>
変数に格納されている文字列のアポストロフィをエスケープする方法、またはこれを回避する別の方法はありますか?それともアポストロフィが問題ですか? どんな助けでも大歓迎です。ありがとう!
ブライアン