简体   繁体   中英

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

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.

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 (ie 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.

WebDriver代表您的浏览器,WebElement代表网页上的各个元素。为了更好地理解,您可以提供您的代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM