【发布时间】:2015-07-12 00:55:21
【问题描述】:
我正在学习 Selenium Webdriver 并尝试编写一个简单的测试脚本。
目的是获取Gmail页面上的About Google链接,从而练习CSS定位器 .
代码如下:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
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 对象。
如果是这种情况,是否有解决方法/强制转换?
有什么建议吗?
【问题讨论】:
标签: java selenium selenium-webdriver css-selectors