It might be something obvious I've missed, but I've been staring at it for so long now that I decided to post it here_
I have this snipppet of markup for a submit button:
<button class="hb-knapp hb-knapp--standard" id="lastOpp_SOSIALE_UTFORDRINGER_03077000045_soker__"
type="button" data-e2e-selector="lastOpp_SOSIALE_UTFORDRINGER_03077000045_soker__"><!----><hb-ikon
class="hb-knapp-ikon" _nghost-rmd-c56=""><span _ngcontent-rmd-c56="" class="hb-ikon hb-ikon--
focusable="false"></span><!----></hb-ikon><!----><!----><span translate="" class="hb-knapp-
tekst">Last opp vedlegg<!----></span><!----><!----></button>
I'm trying to verify the visibility of the button by using "beginning with", since the number towards the end of the id and data-e2e-selector is dynamic.
So I'm trying this:
return driver.findElement(By.cssSelector("[id^=lastOpp_SOSIALE_UTFORDRINGER_]")).isDisplayed();
and this:
return driver.findElement(By.cssSelector("[data-e2e-selector^=lastOpp_SOSIALE_UTFORDRINGER_]")).isDisplayed();
Both yields the "Unable to locate element" error:
Unable to locate element: {"method":"css selector","selector":"[id^=lastOpp_SOSIALE_UTFORDRINGER_]"}
What on earth am I not seeing here? As far as I can see, I'm looking for the exact same locator(s) that are plainly visible in the markup?
EDIT:
I also tried using xpath, like this
return driver.findElement(By.xpath("//*[contains(@data-e2e-selector, 'lastOpp_SOSIALE_UTFORDRINGER_')]")).isDisplayed();
and this
return driver.findElement(By.xpath("//*[contains(@dataid, 'lastOpp_SOSIALE_UTFORDRINGER_')]")).isDisplayed();
with the exact same result. This seems completely bonkers.
EDIT2:
An also, of course, as a last test: The exact id:
boolean is = driver.findElement(By.id("lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__")).isDisplayed();
also returns "element not found".
The strange thing is - other elements on the same page are both visible and interactable.
EDIT3:
Tried adding a manual WebDriverWait, but even then Selenium insists on not finding the element:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__")));
or
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='lastOpp_SOSIALE_UTFORDRINGER_13108100073_soker__']")));
(The xpath is copied directly from the browser - Chrome.)
Result:
Expected condition failed: waiting for visibility of element located by By.id: lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__ (tried for 5 second(s) with 500 milliseconds interval)
And I'm watching the page, staring at the element in question, as the wait times out.
EDIT4: Snapshot of the html markup