【问题标题】:CSS Locator with contains() InvalidSelectorException using Selenium WebDriverCSS Locator with contains() InvalidSelectorException 使用 Selenium WebDriver
【发布时间】: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


    【解决方案1】:

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

    在 gmail 之类的网站上工作也不好。

    【讨论】:

    • 嗯,这就是我想确认的一点。我正在尝试 IDE 中的定位器,但我使用的定位器不起作用。顺便说一句,我从saucelabs.com/resources/selenium/css-selectors(Inner Text)中选择了列表,并认为它们也适用于 webdriver 脚本。
    • @SandeepDharembra contains() 不适用于 css 定位器和 selenium Web Driver。有关更多信息,请参阅我的答案。
    • 感谢@nazar_art 的投入
    • seleniumtrainer.com 上不再有任何页面 - 该 URL 现在从 GoDaddy 出售。该页面是否仍然存在,如果存在,您能否更新链接?
    • 嗨..目前它已关闭..对此感到抱歉..一旦它启动会更新您。
    【解决方案2】:

    主要问题出在这一行:

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

    css 不为 Selenium WD 维护 contains() - See here

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

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

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

    对于驱动程序,它将是:

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

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

    driver.findElement(By.linkText("你的链接名"));

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

    • 您不能使用 css 选择器获取父元素(Xpath 具有 xpath 轴)
    • 你不能使用 contains(它只是 xpath 权限)。

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

    【讨论】:

    • 其实“包含”是通过 *= 在 css 中完成的,例如[attribute*='value'] 同样,^= 和 $= 的意思分别以开头和结尾。
    • @JonNelson 我误解了你的意思。你能展示一些与问题情况相关的示例用法吗?用于替换此 xpath - //a[contains(text(), 'About Google')]
    • 我注意到 Selenium 在内部将 By.cssSelector("div[data-testid ^='avatar']") 转换为 By.xpath("//div[contains(data-testid,'avatar')]")
    【解决方案3】:

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

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

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

    并像使用它一样

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

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 2023-01-03
      • 2014-06-17
      • 2018-03-06
      • 2015-01-30
      • 2018-11-30
      • 2021-04-14
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多