Selenium cannot find by id

Asked

Viewed 2,850 times

2

I’m running a test with testNG along with Selenium

org.openqa.selenium.By

Test, do this search:

driver.findElement(By.id("idRoleCheck:0")).click();

Gives the following error:

org.openqa.Selenium.Elementnotvisibleexception: element not Visible

Analyzed with the Chrome development tool, you have the Id, I can see it, but I think it’s this character’s problem ":" , but I don’t know, I’ve tried several searches around here and other places and nothing.

I’m using the driver:

org.openqa.selenium.chrome.ChromeDriver

Someone’s been through it?

HTML generated in the browser:

<div class="ui-helper-hidden-accessible">
    <input id="idRoleCheck:0" name="idRoleCheck" value="value"
     onchange="PrimeFaces.ab({s:&quot;idRoleCheck&quot;,e:&quot;change&quot;,p:&quot;idRoleCheck&quot;,u:&quot;idRoleCheck&quot;});" 
    type="checkbox">
</div>

None of the answers so far have solved. But evolving we managed to pass the test.

driver.findElement(By.xpath("//table[@id='idRoleCheck']/tbody/tr/td/div/div[2]/span")).click();

The problem in reading the id in the way questioned continues, maybe it is missing some dependency that this generating the problem, because the log, never comes with much information (or it is anyway).

Using:

How the problem was found

Using:

  • Selenium IDE (that firefox plugin to write test data)
  • Exported to type (Testng / Java /Webdriver)
  • It generates two lines, the first that gives the error (idRoleCheck:0)
  • The second reading by By.xpath(...
  • Dude, I only used Selenium with Python but, have you ever tried to use it to catch with xpath instead of id? Or does it have to be ID msm? I think the xpath is more reliable, is more straight you know? Since you’re trying for ID and not rolling, maybe the xpath will work!

4 answers

1

I had a similar problem and managed to solve with the following function

Try it this way:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript( "setTimeout(function(){ document.getElementById('idRoleCheck:0').value = 'value'; }, 1000)");

if somewhere you have a data-bind referenced to that id, use it as follows:

js.executeScript(
            "setTimeout(function(){ model."o que tiver dentro do seudata-bin"('o valor que você quer selecionar'); document.getElementById('idRoleCheck:0').value = 'value'; }, 1000)");

0

Try to put two backslashes before the :, like this:

driver.findElement(By.id("idRoleCheck\\:0")).click();

0

Why don’t you search all the elements of the kind checkbox and then click on the element you want, it’s always the easiest way.

Kind of:

List<WebElement> checkboxes = 
driver.findElements(By.cssSelector("input[type*=\"checkbox\"]"));
checkboxes.get(0).click();

0

I’ve had many problems of this kind, especially with IE. You can try to find the element by attribute.

By.cssSelector("input[id=idRoleCheck:0]");
  • But I don’t know, the link too, see is input, like type="checkbox" Have any suggestions as to what the link would be for this case?

  • I updated the answer, that should do it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.