5

I was using WebElement.findElement(By.cssSelector('')).click(); to find an element on a page , but it returned "Unable to locate element", But when I used WebDriver.findElement(By.cssSelector('')).click(); then it was able to find the element and clicked on it.

I am unable to understand the difference between the findElement() of the two interfaces. Kindly Help.

2
  • those who suggested the edits ... can you please suggest some differences also !! :P Commented Nov 12, 2014 at 9:01
  • WebElement and WebDriver are interfaces which contain abstract methods to be overridden by the class which implements them. The way you are using them doesn't make sense. Can you please provide the code you have been using to implement the above ? Commented Nov 12, 2014 at 10:34

2 Answers 2

9

WebElement.findElement() will use the element as the scope in which to search for your selector. This means it is generally used for searching for child elements.

WebDriver.findElement() will use the driver (i.e the entire page) to search for your given selector.

So for your instance specifically (and we'll need more information to figure it out more accurately) it's more than likely your selector doesn't match anything that is a child of your given WebElement, but when the driver tries to search for it in the entire page (excluding iframes), it can find it.

Sign up to request clarification or add additional context in comments.

Comments

0

WebDriver represents your browser and WebElement represents individual elements present on web page.for better understanding can u provide your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.