Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught DOMException: Failed to execute '$' on 'CommandLineAPI': not a valid selector

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?

like image 576
Prateek Saxena Avatar asked Nov 07 '22 07:11

Prateek Saxena


1 Answers

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']"
    
like image 55
undetected Selenium Avatar answered Dec 03 '22 15:12

undetected Selenium