Reputation: 79
The following Code fails in IE and Firefox. Never had a problem with Chrome.
foundElement = driver.FindElement(By.Id("btn-GDR"));
It says couldn't find the element #btn\-GDR
Why is Selenium inserting a \
before the -
?
Firefox 65.0.2 Version IE 11.0.9600.19301
EDIT: More Info: I've tried using
"btn\x2dGDR" meaning \x2d is the "-" symbol (ASCII in HEX) but it does not solve the problem. It always insert a "\" before it.
Upvotes: 4
Views: 2090
Reputation: 193088
As Selenium converts the different Locator Strategies into it's effective CSS selectors as per the switch - cases the values of class name, id, name, tag name, etc are converted through:
cssEscape(value);
The cssEscape(value)
is defined as:
private String cssEscape(String using) {
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
using = "\\" + Integer.toString(30 + Integer.parseInt(using.substring(0,1))) + " " + using.substring(1);
}
return using;
}
Hence you see the -
character being escaped by the \
character.
Upvotes: 1
Reputation: 79
I will answer my own question since I've found the solution. I added a wait before finding the element.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.
PresenceOfAllElementsLocatedBy(By.Id("btn-GDR")));
Turns out that sometimes the element is not present for some strange reason.. I can see it on screen but it takes 2-3 secs for Selenium to properly being able to interact with it. Yes, the element is always visible, enabled and it does exits. Also, when reporting the options Selenium reports adds the backslash before the hyphen to the output message.
FYI I've found the same case here. It was unanswered. Similar Problem
Upvotes: 0