什么是CSS Selector路径来查找text()

时间:2016-06-17 13:41:49

标签: selenium css-selectors

我正在使用

WebElement AdministratorMessage=driver.findElement(By.cssSelector("div:contains('Your new administrator(s) have been created and invitation email(s) sent')"));

<div class="saveMsg nd___highlighted" ng-show="showBanner==true" style="display: none; outline: rgb(204, 0, 0) dashed 2px;">
<i class="fa fa-check" style="outline: rgb(255, 255, 255) none 0px;"></i>
                Your new administrator(s) have been created and invitation email(s) sent
                <i class="fa fa-close msgClose" ng-click="hideSuccessMsg()"></i>
            </div>

错误讯息:

  

org.openqa.selenium.InvalidSelectorException:无效的选择器:An   指定了无效或非法的选择器(会话信息:   chrome = 48.0.2564.116)(驱动信息:chromedriver = 2.19.346078   (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform = Windows NT 6.3   x86_64)(警告:服务器未提供任何堆栈跟踪   信息)命令持续时间或超时:31毫秒   有关此错误的文档,请访问:   http://seleniumhq.org/exceptions/invalid_selector_exception.html建立   信息:版本:&#39; 2.48.2&#39;,修订版:   &#39; 41bccdd10cf2c0560f637404c2d96164b67d9d67&#39;,时间:&#39; 2015-10-09   13:08:06&#39;系统信息:主持人:&#39; INDIA-DEV36&#39;,ip:&#39; 192.168.186.56&#39;,   os.name:&#39; Windows 8.1&#39;,os.arch:&#39; amd64&#39;,os.version:&#39; 6.3&#39;,   java.version:&#39; 1.8.0_60&#39;司机信息:   org.openqa.selenium.chrome.ChromeDriver功能   [{applicationCacheEnabled = false,rotate = false,   mobileEmulationEnabled =假,   铬= {userDataDir = C:\用户\ HARSH〜1.SHA \应用程序数据\本地\ TEMP \ scoped_dir8680_6077},   takesHeapSnapshot = true,databaseEnabled = false,handlesAlerts = true,   hasTouchScreen = false,版本= 48.0.2564.116,platform = WIN8_1,   browserConnectionEnabled = false,nativeEvents = true,   acceptSslCerts = true,locationContextEnabled = true,   webStorageEnabled = true,browserName = chrome,takesScreenshot = true,   javascriptEnabled = true,cssSelectorsEnabled = true}]会话ID:   cfd3e5b7a892aca75855ba4299c03bac   ***元素信息:{使用= css选择器,值= div:包含(&#39;您的新管理员已创建,邀请电子邮件已发送&#39;)} at   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:422)     在   org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)     在   org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)     在   org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)     在   org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)     在   org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:492)     在org.openqa.selenium.By $ ByCssSelector.findElement(By.java:430)at   org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)     在   com.atlassian.webdriver.DefaultAtlassianWebDriver.findElement(DefaultAtlassianWebDriver.java:232)     在   com.client.app.pages.AdministratorHome.isAdministratorEmailMessageDisplayed(AdministratorHome.java:283)     在   com.client.app.administrator.CreateAdministratorCondecoSenseStepDefs.verify_SucessMessageDisplayed(CreateAdministratorCondecoSenseStepDefs.java:129)     在✽。然后成功创建新管理员   US171AutoEmail @ condecosoftware.com,您的新管理员已经   创建和邀请电子邮件   发送中(src /测试/资源/ COM /客户端/应用程序/ sense_administrator / CreateAdministratorCondeco.feature:22)

3 个答案:

答案 0 :(得分:1)

您正在使用此选择器:

By.cssSelector("div:contains('Your new administrator(s) have been created and invitation email(s) sent')")

css selector不支持contains。因此,您收到错误消息,指出selector无效。

如果您想使用contains,则应使用xpath

By.xpath("//div[contains(text(), 'Your new administrator(s) have been created and invitation email(s) sent')])

答案 1 :(得分:0)

如果你想尝试使用xpath找到它,试试这个......

By.xpath("//span[text()='Text']")

答案 2 :(得分:0)

您无法使用CSS选择器在查找中搜索文本。您必须找到元素并搜索所需的文本。

WebElement administratorMessage = null;
String searchText = "Your new administrator(s) have been created and invitation email(s) sent";
List<WebElement> divs = driver.findElements(By.cssSelector("div.saveMsg.nd___highlighted"));
for (WebElement div : divs)
{
    if (div.getText().contains(searchText))
    {
        administratorMessage = div;
        break;
    }
}

if (administratorMessage != null)
{
    // searchText found
}
else
{
    // searchText NOT found
    System.out.println("No element was found containing the message, \"" + searchText + "\"");
}