0

In the if-else statement, the program shows the error

no such element: Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Frizerul')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='sg-actions list__hole']//li[2]//span[contains(.,'raportată ca abuz')]"}

and doesn't execute the else (in this case showing a message in console)

The method is:

public void reported(String username){
    if(driver.findElement(By.xpath("//a[contains(.,'"+username+"')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='sg-actions-list__hole']//li[2]//span[contains(.,'raportată ca abuz')]")).isDisplayed()==true)
    {
        System.out.println ("Tema raportata " + driver.getCurrentUrl()); 
    }
    else{
        System.out.println ("Tema nneraportata"+driver.getCurrentUrl());
    }
}

Can anyone explain me what I am doing wrong?

PrimeOfKnights
  • 426
  • 5
  • 17
ctina
  • 51
  • 1
  • 10
  • Possible duplicate of [Selenium WebDriver - Test if element is present](https://stackoverflow.com/questions/7991522/selenium-webdriver-test-if-element-is-present) – JeffC Aug 17 '17 at 15:30

3 Answers3

0

I think this element is not available in the moment the code is executed.

Is this lazy loaded content? Are you sure that element it's in html? Do you use browser tools for writing xpath?

Chrome/Firefox has great tools to help you write Xpath code.

John Tribe
  • 1,407
  • 14
  • 26
  • i am using chrome and if i want to test an xpath i am just using Ctrl+f and write the path in the search field. If you can recommend some good tools it would be great – ctina Aug 17 '17 at 14:37
  • I always used xpath-helper (chrome plugin) . It highlight elements that xpath find (check your xpath with it). Maybe your problem is that the element is added later to html (ajax call). Add wait (5-20 seconds) before if. – John Tribe Aug 17 '17 at 14:44
  • the element appears when the page is loaded. – ctina Aug 17 '17 at 14:46
  • "appears" that's mean it's not from the start ? If that's so then you need wait for page fully loaded or wait until element is found (while (isNotPresent())) {Thread.sleep(5000)). In our selenium tests if element was not present we checked 5 more times with 5 sec wait and if it still not show then we throw Exception. – John Tribe Aug 17 '17 at 14:52
0

The solution that i found and workes was to place inside else another if. The method looks like this:

public void reported(String username){

if(driver.findElement(By.xpath("//a[contains(.,'"+username+"')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='sg-actions-list__hole']//li[2]")).getText().contains("raportată ca abuz"))
{
    System.out.println ("Tema raportata " + driver.getCurrentUrl()); 
} 
else{
    if(driver.findElement(By.xpath("//a[contains(.,'"+username+"')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='sg-actions-list__hole']//li[2]")).getText().contains("Raportează abuzul!"))
    {
        System.out.println ("Tema neraportata " + driver.getCurrentUrl());
    }
    }

}
ctina
  • 51
  • 1
  • 10
0

Try this:

public void reported(String username){
    String elementXpath = "//a[contains(.,'"+username+"')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='sg-actions-list__hole']//li[2]//span[contains(.,'raportată ca abuz')]";

    WebElement ele = driver.findElement(By.xpath(elementXpath));        

    if(null == ele || ele.isDisplayed()==false)
    {
        System.out.println ("Tema nneraportata"+driver.getCurrentUrl());
    }
    else{
        System.out.println ("Tema raportata " + driver.getCurrentUrl()); 

    }
}
san1deep2set3hi
  • 4,904
  • 1
  • 23
  • 23