1

Webpage i am automating: https://app.ghostinspector.com/account/create

Scenario: I am clicking on the signup page and entering details and clicking on the register button, now if user passes a same email address the message "E-mail address is already in use." is displayed on the website so what i am trying to do is that locate the text message clear it and enter another email address at runtime.

Now the problem is that the error message text is not fetched by the gettext method of selenium.

Below is the code:

 WebElement email_in_use = driver.findElement(
    By.xpath("/html/body/div[1]/div/div/div[2]/form/div/div[1]"));
    String message = email_in_use.getText();    
    System.out.println(message);

Let me know whats the problem here.

2
  • Try to use better xpath expression rather than automated tools generated xpath expressions. check w3schools.com/xml/xpath_intro.asp Commented Feb 1, 2018 at 11:10
  • @AmrLotfy Thanks for your advice but this question was put a year back if u didn't miss out the post date :) Was a beginner in this at that time. Commented Feb 2, 2018 at 12:12

5 Answers 5

1

You just need a brief wait to wait until the DIV that contains the error message appears. The code below is working for me.

WebDriver driver = new FirefoxDriver();
driver.get("https://app.ghostinspector.com/account/create");
driver.findElement(By.id("input-firstName")).sendKeys("Johnny");
driver.findElement(By.id("input-lastName")).sendKeys("Smith");
driver.findElement(By.id("input-email")).sendKeys("[email protected]");
driver.findElement(By.id("input-password")).sendKeys("abc123");
driver.findElement(By.id("input-terms")).click();
driver.findElement(By.id("btn-create")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement e = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div[ng-show='errors']")));
System.out.println(e.getText());
Sign up to request clarification or add additional context in comments.

Comments

0

you are using getAttribute("value") to get the text from the div element. try using email_in_use.getText().

4 Comments

yup Sudharshan had tried gettext() method initially it didnt work.
it is working fine for me. try adding Thread.sleep(2000) after clicking the create account button and get the text from the div.
@SudharsanSelvarj Thread.sleep is not a better solution to achieve it you need to implement WebDriverWait with ExpectedCondition...:)
yeah i know @SaurabhGaur .Just for checking i asked to try using Thread.sleep.
0

You need to implement WebDriverWait here to getText() because the error element already present there without any text and it fill with text when any error occurred like E-mail address is already in use.

So you need to wait until error element has some text as like below :-

WebDriverWait wait = new WebDriverWait(driver, 100);

String message = wait.until(new ExpectedCondition<String>() {
                    public String apply(WebDriver d) {
                        WebElement el = d.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/form/div/div[1]"));
                        if(el.getText().length() != 0) {
                            return el.getText();
                        }
                    }
                });

System.out.println(message);

Note :- As your xpath is positional which is depends on element position, it's may be fail if there wiil be some element add during action, I suggest, you can be use this xpath also By.xpath("//div[@ng-show='errors']").

You can also use as like below :-

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@ng-show='errors']"), "E-mail address is already in use"));
WebElement email_in_use = driver.findElement(By.xpath("//div[@ng-show='errors']"));
String message = email_in_use.getText();

Hope it will work....:)

Comments

0

Try usinfg this to clear the text box

driver.switchTo().alert().getText(); 

3 Comments

Thats not the issue. I am not able to get the text "E-mail address is already in use." from the webelement email_in_use ....
Sure. <div class="alert alert-warning ng-binding" ng-show="errors">E-mail address is already in use.</div>
driver.switchTo().alert().getText(); This will get the text from alert you are getting.
0

Have you tried element.getAttribute('value') ?

src

Comments

Your Answer

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.