Java 使用Selenium WebDriver的CSS定位器,包含()InvalidSelectorException

Java 使用Selenium WebDriver的CSS定位器,包含()InvalidSelectorException,java,selenium,selenium-webdriver,css-selectors,Java,Selenium,Selenium Webdriver,Css Selectors,我正在学习SeleniumWebDriver,并试图编写一个简单的测试脚本 目的是在Gmail页面上获取关于Google的链接,以便练习CSS定位器 代码如下: import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public cla

我正在学习SeleniumWebDriver,并试图编写一个简单的测试脚本

目的是在Gmail页面上获取
关于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对象

如果是这种情况,是否有解决方案/铸造


有什么建议吗?

例外情况是,这里的问题是您的Css选择器无效。”您正试图获取关于Google的
锚定标记,因为它的文本不是有效的css选择器。它更像是一个jQuery选择器

您可以根据href属性的值使用选择器,如下所示,它可以正常工作

 #footer-list a[href*='about']
像这样使用它

WebElement aboutGoogle = driver.findElement(By.cssSelector("#footer-list a[href*='about']"));

主要问题在这条线上:

driver.findElement(By.cssSelector(“a:contains('About Google'))))

css
不会为Selenium WD-维护
contains()

要使用
contains()
必须使用

使用Xpath您的定位器将是:

//a[包含(text(),“关于谷歌”)]

对于驾驶员,它将如下所示:

findElement(By.xpath(“//a[contains(text(),'About Google')]”)

要查找与Selenium的链接,您可以使用:

driver.findElement(By.linkText(“此处的链接名”)

与Xpath相比,CSS选择器存在局限性:

  • 不能使用css选择器获取父元素(Xpath具有Xpath轴)
  • 不能使用contains(这只是xpath权限)
顺便说一句
要处理页面中的Xpath定位器,您可以使用Firefox浏览器扩展:


CssSelector
在脚本中不起作用,但在selenium IDE中起作用


在gmail这样的网站上工作也不好。

这就是我想确认的一点。我在尝试IDE中的定位器,但我使用的定位器不起作用。顺便说一句,我从(内部文本)中选择了该列表,并认为它们也适用于Web驱动程序脚本。@SandeepDharembra
contains()
不适用于css定位器,适用于selenium Web驱动程序。更多信息请参见我的答案。感谢@nazar_art的输入。seleniumtrainer.com上不再有任何页面-该URL现在由GoDaddy出售。该页面是否仍然存在,如果存在,您可以更新链接吗?嗨。目前该页面已关闭。。对不起。。一旦启动,它将更新您。实际上,“包含”是通过*在css中完成的,例如[attribute*='value'],类似地,^=和$=平均值分别以开头和结尾。@Joni误解了您的观点。你能展示一些与问题情境相关的示例用法吗?用于替换此xpath-
//a[contains(text(),'About Google')]