Unable to locate element using cssSelector and xpath

301 Views Asked by At

I hope you can help me, I'm using selenium web driver and I want to locate a specific element in a website. I tried using cssSelector and xpath but I'm having a InvalidSelectorException and NoSuchElementException. Please see screenshot below for more details.

I'm trying to locate the BACK TO HOME button

By using firebug I was able to see the underlying HTML code of the BACK TO HOME button

I'm trying to locate the BACK TO HOME button. By using firebug I was able to see the underlying HTML code of the BACK TO HOME button. I'm not familiar on how to handle the highlighted HTML code since most of the time a link usually has a name and a cssSelector=linkText("link name") would solve the problem and a quick copy xpath command can easily locate the element. But on this scenario both cssSelector and xpath did not worked.

How do can I locate this kind of HTML code?

2

There are 2 best solutions below

4
On

You can try XPath as

//img[contains(@src,'home.gif')]

Since here contains method is used you can use any substring of src attribute.

0
On

I had already solved this problem. The problem was not on the xpath, it was on the code. I was trying to locate an element who is not in the page. I put my xpath code on the page where the element resides.