Why does Selenium inserts a backslash before the Hyphen character? Why does Selenium inserts a backslash before the Hyphen character? selenium selenium

Why does Selenium inserts a backslash before the Hyphen character?


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.


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