Java CSS Locator with contains() InvalidSelectorException 使用 Selenium WebDriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29999630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:56:16  来源:igfitidea点击:

CSS Locator with contains() InvalidSelectorException using Selenium WebDriver

javaseleniumselenium-webdrivercss-selectors

提问by Sandeep Dharembra

I am learning Selenium Webdriverand trying to write a simple test script.

我正在学习Selenium Webdriver并尝试编写一个简单的测试脚本。

The intent is to get the About Googlelink on Gmailpage so as to practice CSS locators.

目的是获取Gmail页面About Google上的链接,以便练习CSS 定位器

Here is the code:

这是代码:

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();          
    }    
}

I get the below mentioned exception:

我得到以下提到的异常:

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

I had checked and was able to find the element in Selenium IDE using the same locator.

我已经检查并能够使用相同的定位器在 Selenium IDE 中找到该元素。

I read somewhere that the findElement()method is returning a DOM node and the code is expecting a WebElement object.

我在某处读到该findElement()方法正在返回一个 DOM 节点,而代码需要一个 WebElement 对象。

If this is the case then, is there a workaround/casting?

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

Any suggestions?

有什么建议?

采纳答案by murali selenium

CssSelector with contains text does not work in scripting but it works in selenium IDE.

包含文本的 CssSelector 在脚本中不起作用,但它在 selenium IDE 中起作用。

One more is, its not good to work on sites like gmail.. you can learn by using components in http://seleniumtrainer.com/. its good for start up.

还有一点是,在 gmail 等网站上工作并不好。你可以通过使用http://seleniumtrainer.com/ 中的组件来学习。它有利于启动。

thank you

谢谢你

回答by Anirudh

Well as the Exception is clearly stating the problem here is that your Css Selector is not valid. 'You are trying to get the About Googleanchor tag based on it's text which is not a valid css selector'. It's more of a jQuery selector.

异常清楚地说明这里的问题是您的 Css 选择器无效。'您正试图根据它的文本获取About Google锚标记,该文本不是有效的 css 选择器'。它更像是一个 jQuery 选择器。

You could use the selector based on the value of href attribute as shown below and it will work fine.

您可以使用基于 href 属性值的选择器,如下所示,它会正常工作。

 #footer-list a[href*='about']

and use it like

并像使用它一样

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

回答by nazar_art

The main problem is at this line:

主要问题是在这一行:

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

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

cssdoesn't maintain contains()for Selenium WD - See here.

css不维护contains()Selenium WD -请参阅此处

For using contains()you have to use Xpath.

要使用,contains()您必须使用Xpath

With Xpathyour locator will be:

使用Xpath,您的定位器将是:

//a[contains(text(), 'About Google')]

//a[contains(text(), '关于谷歌')]

and for driver it will be as:

对于驱动程序,它将是:

driver.findElement(By.xpath("//a[contains(text(), 'About Google')]"));

driver.findElement(By.xpath("//a[contains(text(), '关于谷歌')]"));

For finding links with Seleniumyou can use:

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

driver.findElement(By.linkText("your link name here"));

driver.findElement(By.linkText("你的链接名称在这里"));

It is limitation of CSSselectors compare to Xpath:

Xpath相比,这是CSS选择器的局限性:

  • you can't take parent element with css selectors (Xpath has xpath axes)
  • you can't use contains (it is only xpath privilege).
  • 你不能用 css 选择器来获取父元素(Xpath 有 xpath 轴)
  • 你不能使用 contains(它只是 xpath 权限)。

BTW
For processing Xpathlocators from page you able to use extension for Firefox browser:

顺便说一句,要从页面
处理Xpath定位器,您可以使用 Firefox 浏览器的扩展: