harsh sharma
harsh sharma

Reputation: 75

What is the CSS Selector path to find text()

I am using

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>

Error Message:

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=48.0.2564.116) (Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 31 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06' System info: host: 'INDIA-DEV36', ip: '192.168.186.56', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_60' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\HARSH~1.SHA\AppData\Local\Temp\scoped_dir8680_6077}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=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}] Session ID: cfd3e5b7a892aca75855ba4299c03bac *** Element info: {Using=css selector, value=div:contains('Your new administrator(s) have been created and invitation email(s) sent')} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363) at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:492) at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355) at com.atlassian.webdriver.DefaultAtlassianWebDriver.findElement(DefaultAtlassianWebDriver.java:232) at com.client.app.pages.AdministratorHome.isAdministratorEmailMessageDisplayed(AdministratorHome.java:283) at com.client.app.administrator.CreateAdministratorCondecoSenseStepDefs.verify_SucessMessageDisplayed(CreateAdministratorCondecoSenseStepDefs.java:129) at ✽.Then New Administrator created successfully [email protected],Your new administrator(s) have been created and invitation email(s) sent(src/test/resources/com/client/app/sense_administrator/CreateAdministratorCondeco.feature:22)

Upvotes: 0

Views: 4892

Answers (3)

JeffC
JeffC

Reputation: 25611

You can't search for text in the find using CSS selectors. You will have to find the element(s) and search for the desired text.

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 + "\"");
}

Upvotes: 0

Anthony
Anthony

Reputation: 80

If you want to try to find it using xpath, try this...

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

Upvotes: 1

RemcoW
RemcoW

Reputation: 4326

You are using this selector:

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

css selector does not support contains. Therefore you are getting an error saying the selector is invalid.

If you want to use contains you should make use of xpath.

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

Upvotes: 3

Related Questions