0

I am trying to locate an element using Chrome browser. The problem is when I try to locate with the fresh instance of the browser, elements are not located. Now when I manually inspect the element and then try to locate in console using same xpath/id/name expression, I am able to locate that. I have attached the screenshot. Please suggest some work around.

Failed Attampe to locate the element

Successful attempt

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Now it is making my selenium script to fail every time I am running. I get NoSuchElementException which I understand why its getting. How to enable my browser to locate the element without manually doing it first? – Prashant Shekhar Mar 14 '19 at 07:01
  • Please add the html of the element you are trying to locate – Sameer Arora Mar 14 '19 at 07:02
  • @PrashantShekhar [Don't do this](https://meta.stackoverflow.com/questions/361474/should-we-display-a-warning-when-users-include-images/361481#361481). Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – undetected Selenium Mar 14 '19 at 08:21

1 Answers1

1

The screenshot you attached does answer your question.

Look at the first screenshot at the very top it says "top". It's the context in which you look for the element.

In the second screenshot, you can see that "top" changed to "main (Welcome.jsp)"

It means that the element cannot be found in the main context. It's located in the iframe

To find that element use

driver.switchTo().frame("frame name or frame id");

and then perform .findElement(). WebDriver will look for elements in the iframe.

When you're done, switch to top context with the following code:

driver.switchTo().defaultContent();

Fenio
  • 3,528
  • 1
  • 13
  • 27