Home > Mobile >  Selenium unable to locate an element
Selenium unable to locate an element

Time:09-09

I'm trying to locate an element with Selenium. But it gives an error saying:

Unable to locate an element with the xpath expression //li[normalize-space()='Don't check credit card BINs'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li[normalize-space()='Don't check credit card BINs']' is not a valid XPath expression.

I've tried searching the element by inspect and indeed the element cannot be read/found. Is there a special way on writing code for Can't word? Because I suspect that ' symbol cannot be located by selenium.

Here is the dropdown menu that I want to locate (the Don't check credit card BINs menu): enter image description here

As you can see above, that the specific text is not found by xpath. enter image description here

CodePudding user response:

You are missing "" in your xpath, as I can see this in you HTML source, also to ignore apostrophe in Don't you can try below

"//li[normalize-space()=\"Don't check credit card BINs\"]"

If this does not work you can try below one too.

"//li[contains(text(),\"Don't check credit card BINs\")]"

CodePudding user response:

Here are some things you can try.

  1. Try using javascript executor, happened to me a few times I can't click a dynamically loaded element with selenium builtin selectors but I was able to click them with selenium js executor

  2. Use absolute xpath, helpful where relative xpath / css selector fails. You can use chrome extensions to get absolute xpath

Hope this helps.

  • Related