6

I am writing automation UI tests for my web page using selenium. I have an element on the web page which I am testing:

<&lt input type="checkbox" id="screening_questions[0].multiple_choice[0]-dealbreakerField" value="on" style="position: absolute; cursor: inherit; pointer-events: all; opacity: 0; width: 100%; height: 100%; z-index: 2; left: 0px; box-sizing: border-box; padding: 0px; margin: 0px;>

Since the element has id attribute, so I tried to locate it by using its id value but it didn't work.

If I search for that element in chrome console as:

$('#screening_questions[0].multiple_choice[0]-dealbreakerField')

I get the exception: Uncaught DOMException:

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.

I thought it will be pretty straight forward to locate it given its id value. Could you please suggest what could be wrong here?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

This error message...

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.

...implies that the Locator Strategy you have adapted is not a valid selector.

As per the HTML you have shared the desired element is a <input> tag with type attribute as checkbox and to use the id attribute you have to escape the . characters and you can use either of the following options :

  • cssSelector :

    "input[id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][type='checkbox']"
    
  • xpath :

    "//input[@id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][@type='checkbox']"
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352