Selenium - Difference between WebDriver.findElement() and WebElement.findElement()

北城余情 提交于 2020-05-08 05:26:29

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/26882604/selenium-difference-between-webdriver-findelement-and-webelement-findelement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!