I want to get an element by ID with selenium in JAVA. The ID of the element in question so happens to have a period in it id="myprojectId0.123456789"
when I try to find the element like so
WebElement projId = driver.findElement(By.id("mprojectId0.10724909303153396"));
I get this error in the console:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #mprojectId0\.10724909303153396
For some reason the .
character within the value of the ID attribute is being converted to \.
and hence it cannot find the element. Please help and thanks in advance!
Your observation is pretty much justified and as expected. As per the discussion in Official locator strategies for the webdriver
By.id
is translated by Selenium into it's equivalentBy.cssSelector
and as the.
character is a special character, it is automatically escaped by a backslash i.e.\
. Hence:gets translated into
Solution
However the value of the
id
attribute i.e.mprojectId0.10724909303153396
looks dynamic to me and would change everytime the HTML DOM gets rendered. Accordingly you have to induce WebDriverWait for thevisibilityOfElementLocated()
and you can use either of the following dynamic Locator Strategies:cssSelector
:xpath
: