1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Myzizo
  • 19
  • 3

2 Answers2

0
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"));
Vavaste
  • 139
  • 1
  • 5
  • I've seen the editet question, this should work. The css selector will not because the elemant has no class, the xpath is a bit more complex because we might need the full page to provide you an answer. – Vavaste Aug 18 '22 at 10:46
  • I have added the snippet of the HTML , the solution provided didn't work either. Thank you so much. – Myzizo Aug 18 '22 at 10:58
  • edited, try with the last code snippet that i've provided. – Vavaste Aug 18 '22 at 11:02
0

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']"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352