1

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

enter image description here

  • try single quotes around lastOpp_SOSIALE_UTFORDRINGER_ or try to select it by xpath – JavaMan Nov 10 '20 at 15:40
  • I tried xpath as as well, as per the updated main post. But it still isn't found. –  Nov 10 '20 at 16:19
  • is your element immediqtely visible after the page load? ...if it gets loaded after the page load take a look at https://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java. Or is your element nested inside a iframe? And check with a XPath plugin for the Browser if you manage to locate the element with the plugin – JavaMan Nov 10 '20 at 18:20
  • It's visible immediately. See EDIT3 above. I'm looking right at the element while Selenium times out trying to find it. No iFrame. I've copied the xpath from Chrome, as used in EDIT 3. Nothing seems to help –  Nov 10 '20 at 19:13

1 Answers1

0

Based on https://sqa.stackexchange.com/a/7889 and https://stackoverflow.com/a/39594489, I think the problem is that you need specify that you're looking for a button.

Try the following:

driver.findElement(By.cssSelector("button[id^=lastOpp_SOSIALE_UTFORDRINGER_]"))
theonlyrao
  • 416
  • 2
  • 13