问题描述
我使用 WebElement.findElement(By.cssSelector('')).click();
在页面上查找元素,但它返回 无法定位元素"
,但是当我使用 WebDriver.findElement(By.cssSelector('')).click();
然后它能够找到该元素并点击它.
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.
我无法理解两个接口的 findElement()
之间的区别.请帮助.
I am unable to understand the difference between the findElement()
of the two interfaces.Kindly Help.
推荐答案
WebElement.findElement()
将使用元素作为搜索选择器的范围.这意味着它通常用于搜索子元素.
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()
将使用驱动程序(即整个页面)来搜索给定的选择器.
WebDriver.findElement()
will use the driver (i.e the entire page) to search for your given selector.
因此,对于您的实例(我们需要更多信息来更准确地找出它),您的选择器很可能不匹配给定 WebElement
的任何子元素,但是当 driver
试图在整个页面(不包括 iframe)中搜索它时,它可以找到它.
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.
这篇关于Selenium - WebDriver.findElement() 和 WebElement.findElement() 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!