3

Please help me to get the Error message.

<div id="divDraftErrorMessages" data-ng-show="showDraftErrorMessage()" class="alert alert-danger">
 <div data-ng-repeat="message in draftErrorMessages" class="ng-binding ng-scope">
 <img src="../../Images/alert.gif" alt="Error Message">
 Invalid Keyword
 </div>
</div>

I tried with below code but message is printed. Observed failure.

String message = driver.findElement(By.className("ng-binding ng-scope")).getText();
System.out.println(message);

I am struggling as error message is preceded with image. Please help.

The error I get is

org.openqa.selenium.InvalidSelectorException: invalid selector: Compound class names not permitted
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
asked Nov 14, 2017 at 12:12
0

2 Answers 2

2

The problem is that Selenium only allows a single class name, so you can't use "ng-binding ng-scope" as your class selector. You can only use one of the two classes.

I'd suggest starting by getting the outer div by ID since you have an ID to work with. If the inner div is the only div that will display, you can then use findElement(By.tagName("div")).getText(); to retrieve the text.

If that gives you the image HTML as well as the text (it shouldn't), you can use a simple substring function to keep only the text after the >.

So your code should do something like:

outerdiv = driver.findElement(By.id("divDraftErrorMessages"));
message = outerdiv.findElement(By.tagName("div")).getText();
answered Nov 14, 2017 at 12:52
2

ng-binding and ng-scope are 2 different classes. Compound classes are not supported by class selector in selenium. You can use the xpath if want to access the element based on more then one class like below :-

String message = driver.findElement(By.xpath("//div[@class='ng-binding ng-scope']")).getText();
System.out.println(message);

or can be CSS Selector as :

String message = driver.findElement(By.cssSelector("div.ng-binding.ng-scope")).getText();
System.out.println(message);
answered Nov 14, 2017 at 13:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.