我正在学习 Selenium Webdriver 并尝试编写一个简单的测试脚本。
目的是获取 Gmail 页面上的 About Google
链接,以便练习 _CSS 定位器_。
这是代码:
public class GoogleSearch {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
WebElement aboutGoogle = driver.findElement(By.cssSelector("a:contains('About Google')"));
driver.close();
driver.quit();
}
}
我得到下面提到的异常:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector a:contains('About Google') is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal selector was specified
Command duration or timeout: 356 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'XXXXX', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-49-generic', java.version: '1.7.0_79'
*** Element info: {Using=css selector, value=a:contains('Need')}
Session ID: 0f1869f8-c59a-4f61-b1c7-b34ada42573f
Driver info: org.openqa.selenium.firefox.FirefoxDriver
我已经检查并能够使用相同的定位器在 Selenium IDE 中找到该元素。
我在某处读到 findElement()
方法正在返回一个 _DOM 节点_,代码需要一个 WebElement 对象。
如果是这种情况,是否有解决方法/转换?
有什么建议么?
原文由 Sandeep Dharembra 发布,翻译遵循 CC BY-SA 4.0 许可协议
CssSelector
在脚本中不起作用,但在 selenium IDE 中有效。在像 gmail 这样的网站上工作也不好。