4

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.

Fragsman
  • 79
  • 6
  • It's part of css normalization, all the special chars will be appended by `\\`. Which language library you are using. I don't see that issue with python. – supputuri Apr 03 '19 at 19:08
  • Ha ha, I need to put 2 back slashes to show `\\` in the comment. normalization ... – supputuri Apr 03 '19 at 19:09
  • Add a tag for the language you are using... Java? The slash is inserted to escape the hyphen. Have you tried adding a wait? What else have you done to investigate this problem? – JeffC Apr 03 '19 at 20:42
  • I'm using C#. As long as I know if you want to write a \\ you have to escape wth another \\ but not the -. I only have the problem with IE and Firefox WebDriver. – Fragsman Apr 04 '19 at 12:01

2 Answers2

1

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I don't understand the method quite well but for sure that doesn't happen with Chrome. Just for the sake of mark as valid I'd need to know that. Apart from that I haven't found any other solution than asking developers to remove the "-" from the IDs. – Fragsman Apr 10 '19 at 12:03
  • _IE_ and _Firefox_ follows **W3C** dialect where as _Chrome_ follows **OSS** dialect. Your question and this answer is based on _IE_ and _Firefox_ where as _Chrome_ / _ChromeDriver_ may be handling it differently. – undetected Selenium Apr 11 '19 at 10:33
  • I'm sorry my question wasn't 100% correct. The id I am trying to locate is "btn-GDR", and selenium says it cannot find "btn\-GDR", there is only one BACKSLASH before the DASH. I will add more information. – Fragsman Apr 11 '19 at 16:02
  • Update: I've found it works in FF without cssEscape() or anything.. is just a strange condition I'm analyzing. I have like 15 Apps, each one accesible through buttons. I've checked id's for buttons are not repeated. When I enter with an user with 3 buttons visible (due to his access controls) it works! with my user able to access the 15 Apps it doesn't.. so it might be an error with my App or something.. I'll update the post apropiately once I determine the error. – Fragsman Apr 11 '19 at 18:53
  • I SOLVED doing this before finding the element WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.PresenceOfAllElementsLocatedBy(By.Id(locator))); Turns out that sometimes the element is not present by some strange condition.. I can see it on screen but it takes 2-3 secs for selenium to see it. And strangely adds the \- to the output message. – Fragsman Apr 12 '19 at 17:16
0

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

Fragsman
  • 79
  • 6