I've gotten one of the more frustrating Selenium problems.
I've got a table on a page, where one of the elements have a link. The link is called "Send brev".
<td data-e2e-selector="sakHendelser" id="sakHendelser_0" class="ng-star-inserted">
<saksnytt><!----><!----><!---->
<div class="hb-tekst--ingenBryting ng-star-inserted">
<!----><!----><!---->
<button class="hb-knapp hb-knapp--lenke hb-knapp--alignTeks hb-knapp-lenke-nopadding ng-star-inserted"
data-e2e-selector="saksnytt-link">
<i aria-hidden="true" class="fa fa-flag fa-envelope"></i>
<span class="hb-knapp-tekst">Send brev </span></button>
<!----><span aria-hidden="true" class="fa fa-info-circle ng-star-inserted"
id="reservert-info">
</span><!----><!----><!----></div><!---->
</saksnytt></td>
This has worked before, and I haven't found a reason for why it's stopped working now. But no matter how I try to find it, Selenium responds with
no such element: Unable to locate element: {"method":"xpath","selector":"//data-e2e-selector[contains(text(),'Send brev ')]"}
or similar-
I've tried the following:
Browser.Wait().until(presenceOfElementLocated(By.linkText(merknad + " ")));
This times out.
driver.findElement(By.linkText(merknad + " ")).click();
driver.findElement(By.partialLinkText(merknad + " ")).click();
driver.findElement(By.xpath("//data-e2e-selector[contains(text(),'" + "Send brev" +"')]"));
driver.findElement(By.xpath("//id[contains(text(),'" + "Send brev" +"')]"));
data-e2e-selector="sakHendelser" id="sakHendelser
I've also tried adding a space after the "v" in the link text, with no luck.
How can Selenium not find an element that is clearly visible and interactable like this?
Since the code is generated in Angular, and is more or less dynamic, I cannot create a tag for this specific element either, so I'm left with trying to find it by text. Which I cannot get to work.
Any ideas?
I AM able to click it with this code:
driver.findElement(By.xpath("//*[@id='sakHendelser_0']/saksnytt/div/button/span")).click();
But I want to be able to send the link text to the method instead of hardcoding the xpath like that.