CSS Locator with contains()使用Selenium WebDriver的InvalidSelectorException [英] CSS Locator with contains() InvalidSelectorException using Selenium WebDriver

查看:1082
本文介绍了CSS Locator with contains()使用Selenium WebDriver的InvalidSelectorException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

目的是获取 Gmail <上的关于Google 链接/ strong>页面以便练习 CSS定位器

The intent is to get the About Google link on Gmail page so as to practice CSS locators.

以下是代码:

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

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

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

我在某处读到 findElement()方法返回一个DOM节点,代码期待一个WebElement对象。

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

如果是这种情况那么,是否有解决方法/投射?

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

有任何建议吗?

推荐答案

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

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

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

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.

谢谢

这篇关于CSS Locator with contains()使用Selenium WebDriver的InvalidSelectorException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆