小编典典

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

selenium

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

目的是About Google Gmail 页面上获取链接,以便练习 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对象。

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

有什么建议?


阅读 668

收藏
2020-06-26

共1个答案

小编典典

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

还有一个是,它不适用于gmail
..等网站。您可以使用http://seleniumtrainer.com/中的组件进行学习。它对启动很有好处。

谢谢

2020-06-26