0

I have the below list of div tags and I want to select the text 'Entry' from the 3rd div tag.

<DIV class=menuLink style="BORDER-TOP-COLOR: transparent; BACKGROUND: #dcdfec; WHITE-SPACE: nowrap; BORDER-LEFT-COLOR: transparent; BORDER-BOTTOM-COLOR: transparent; BORDER-RIGHT-COLOR: transparent">Start Call</DIV>


<DIV class=menuLink style="BORDER-TOP-COLOR: transparent; BACKGROUND: #dcdfec; WHITE-SPACE: nowrap; BORDER-LEFT-COLOR: transparent; BORDER-BOTTOM-COLOR: transparent; BORDER-RIGHT-COLOR: transparent">Web Pay SR</DIV>


<DIV class=menuLink style="BORDER-TOP-COLOR: transparent; BACKGROUND: #dcdfec; WHITE-SPACE: nowrap; BORDER-LEFT-COLOR: transparent; BORDER-BOTTOM-COLOR: transparent; BORDER-RIGHT-COLOR: transparent">Entry</DIV>


<DIV class=menuLink style="BORDER-TOP-COLOR: transparent; BACKGROUND: #dcdfec; WHITE-SPACE: nowrap; BORDER-LEFT-COLOR: transparent; BORDER-BOTTOM-COLOR: transparent; BORDER-RIGHT-COLOR: transparent">Exit</DIV>

I used

driver.findElement(By.linkText("Entry")).getText();

I get the below error:

Unable to find element with partial link text == Entry

Could some one please help me to sort this out. TIA!

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
The Enthusiast
  • 93
  • 1
  • 11

2 Answers2

2

Try use Webdriverwait and xpath to find dynamic element.

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(.,'Entry')]")));
element.click()

OR

 WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='menuLink'][contains(.,'Entry')]")));
element.click()

Or you can use linkText instead of xpath.

KunduK
  • 32,888
  • 5
  • 17
  • 41
0

As the element is a <DIV> element you can't use linkText or partialLinkText. Hence you see the error as:

Unable to find element with partial link text == Entry

To click() on the element with text as Entry you need to induce WebDriverWait for the elementToBeClickable and you can use either of the following Locator Strategies:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='menuLink' and text()='Entry']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352