Home > Mobile >  Unable to locate elements on a website , i tried all the locators but I get the error "No such
Unable to locate elements on a website , i tried all the locators but I get the error "No such

Time:08-18

Element HTML:

<a href="/cacApp/Main/INBOX" title="View the Inbox" target="main">Inbox</a>

What I tried:

driver.findElement(By.cssSelector("a[text='Log out']"));

then

driver.findElement(By.xpath("//a[.='Log out']"));

Element snapshot:

enter image description hereHTML

CodePudding user response:

driver.findElement(By.linkText("Log out"));

Something like that should work, you should provide the page html for a better response.

My response is based on the fact that in your first try you are saying text='Log out'. findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.

Let me know if it works, otherwise provide me the web page html snippet.

I've seen the actual screen, you must change Log out with Inbox

    driver.findElement(By.linkText("Inbox"));

CodePudding user response:

Given the HTML:

<a href="/cacApp/Main/INBOX" title="View the Inbox" target="main">Inbox</a>

You need to take care of a couple of things here as follows:

  • Within cssSelector doesn't supports the :contains("text") method
  • Within xpath for exact text matches you need to use text()

Solution

To identify the element you can use either of the following locator strategies:

  • Using linkText:

    WebElement element = driver.findElement(By.linkText("Log out"));
    
  • Using cssSelector:

    WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
    
  • Using xpath:

    WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and @title='View the Inbox']"));
    
  • Related