Home > Blockchain >  Selenium C# search with ID attribute results in to CssSelector
Selenium C# search with ID attribute results in to CssSelector

Time:08-13

So, I've got that DIV I wanna get into:

<div id="FromTitle" [something,something]>

And my code for that looks like that:

var searchID = "FromTitle";
//some other code that's finding elements perfectly fine
driver.FindElement(By.Id(searchID));

But VSC spits out exception:

Unhandled exception. OpenQA.Selenium.NoSuchElementExeption: no such element: Unable to locate element: {"method":"css selector","selector":"#FromTitle"}

Why does C#/Selenium looking for CSS and not ID?

I've tried paste value from var into (By.Id("FromTitle")) and got same exception as the one above. I've looked through CSS file, and obviously there's no #FromTitle.For every other previous find elements by Id it works just fine. Even if something's not right I get exception "method":"id". But not for that one.

CodePudding user response:

It is looking for an element by Id, and also by CSS selector.

The selector #FromTitle is the CSS selector to identify an element by Id.

You will either need to use an explicit wait for the element, or switch to the frame in which the element exists. There isn't enough information in your question to give you anything more specific than this.

CodePudding user response:

Barring LinkText, PartialLinkText and XPath Selenium internally converts all the other locator strategies into their equivalent CssSelector.

Hence in the error stacktrace you find CssSelector instead of Id.

The change was propagated through the respective client-specific bindings. For the Selenium-Java clients here is the source code snippet:

case FIND_CHILD_ELEMENT:
case FIND_CHILD_ELEMENTS:
case FIND_ELEMENT:
case FIND_ELEMENTS:
  String using = (String) parameters.get("using");
  String value = (String) parameters.get("value");

  Map<String, Object> toReturn = new HashMap<>(parameters);

  switch (using) {
    case "class name":
      toReturn.put("using", "css selector");
      toReturn.put("value", "."   cssEscape(value));
      break;

    case "id":
      toReturn.put("using", "css selector");
      toReturn.put("value", "#"   cssEscape(value));
      break;

    case "link text":
      // Do nothing
      break;

    case "name":
      toReturn.put("using", "css selector");
      toReturn.put("value", "*[name='"   value   "']");
      break;

    case "partial link text":
      // Do nothing
      break;

    case "tag name":
      toReturn.put("using", "css selector");
      toReturn.put("value", cssEscape(value));
      break;

    case "xpath":
      // Do nothing
      break;
  }
  return toReturn;
  • Related