动机
利用Selenium的CSS选择器机制以及CSS属性选择器和HTML5 data-
自定义属性,用于解决元素的特定挂钩。
问题
使用上面的方法来定位一个分配了CSS类名的元素和 data-
属性,抛出以下异常:
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: The given selector .gs-a-btn["data-value"] is either invalid or does not result in a WebElement. The following error occurred:
[Exception... "An invalid or illegal string was specified" code: "12" nsresult: "0x8053000c (NS_ERROR_DOM_SYNTAX_ERR)" location: "file:///C:/DOCUME~1/eliranm/LOCALS~1/Temp/anonymous6109849275533680625webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5956"]
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:28'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_31'
Driver info: driver.version: unknown
at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/eliranm/LOCALS~1/Temp/anonymous6109849275533680625webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:6537)
相关守则
public void previous(String type) {
By cssSelector = By.cssSelector(".gs-a-btn[data-value='" + type + "']");
driver.findElement(cssSelector).click();
}
我试过了什么
- 用属性选择器查询中的转义双引号替换单引号。
- 指定属性选择器而不是属性值选择器,即
".gs-a-btn[\"data-value\"]"
宁".gs-a-btn[data-value='" + type + "']"
。 查找引用中的信息,例如 硒参考,对CSS属性选择器的任何限制。该文件明确指出:
目前,css选择器定位器支持所有css1,css2和css3 选择器除了css3中的命名空间,一些伪类(
:nth-of-type
,:nth-last-of-type
,:first-of-type
,:last-of-type
,:only-of-type
,:visited
,:hover
,:active
,:focus
,:indeterminate
)和伪 元素(::first-line
,::first-letter
,::selection
,::before
,::after
)。