nick shmick
nick shmick

Reputation: 925

how to recognise specific text with selenium?

I created a short procedure where im clicking automatically on some links in my web, and I want to do this to find if I have error 500.

It looks something like this:

 private def lookForError500 () = {

    println("starting to look for error 500")

    val link1: WebElement = driver.findElement(By.cssSelector("#sidebar > div > ul > li:nth-child(3) > a"))
    link1.click()

    val link2: WebElement = driver.findElement(By.cssSelector("#sidebar > div > ul > li:nth-child(3) > ul > li:nth-child(1) > a"))
    link2.click()

    val link3: WebElement = driver.findElement(By.cssSelector("#sidebar > div > ul > li:nth-child(3) > ul > li:nth-child(2) > a"))
    link3.click()

Now sometimes there are error 500, and when the error 500 page is coming on screen, there is a text I can identify that says "sorry, something went wrong".

How can I look for this text after each click...?

this is how the inspect element looks like on the error 500 page:

enter image description here

Upvotes: 1

Views: 236

Answers (4)

rabia
rabia

Reputation: 106

You can check if a particular element exists this way:

//call the function with the xpath

if(ifElementExistsByXpath("//p[text()='SORRY, SOMETHING WENT WRONG')]"))

{
   
               //do some operation

}

else

{

             //no error..proceed

}

public static boolean ifElementExistsByXpath(String elemXpath)
    
{
            
          int count=driver.findElements(By.xpath(elemXpath)).size();
            
          if(count ==0)
            
          {
                return false;
            
          }
            
          else
            
          {
                return true;
            
          }
    
}

you can also use the class name instead of text.

Upvotes: 0

Chaitanya Pujari
Chaitanya Pujari

Reputation: 367

Hi You can write a function to verify for the 500ERROR Text and call it on every link you click or as per your needs try This if Other Answers above should work until if the Error 500 Text is changing the position you can try this

  public void Verify_Error(){
    String s=driver.getPageSource();
    if(s.contains("Error 500")){

    }else{

    }           
 }

Upvotes: 1

Pawan B
Pawan B

Reputation: 4613

You can get the element By xPath ,css path or by id . Then you can get the text of the element using getText() method.

val text = driver.findElement(By.xPath("/html/body/div/div/div/div/p/strong")).getText();

if(text.equals("500")){
    // something went wrong
}

Upvotes: 0

topahl
topahl

Reputation: 1

I suggest using xPath to identify the <strong class=error-number> element that holds the error number that is 500 in this case. This xPath String would do the thing:

//strong[@class="error-number" and text()="500"]

In Selenium you can use xPath by using the By.xpath() method.

All together it would look like this.

By.xpath("//strong[@class='error-number' and text()='500']")

Upvotes: 0

Related Questions