何时何地使用 By class 的 FindElement 方法
When and where to use FindElement method of By class
我对 Selenium 架构有基本的了解,其中 Search Context 是 Web Driver 接口实现的主要接口,由各种浏览器驱动程序扩展 class。通常,我们在 selenium 项目中遵循 POM,并通过 class -
定义每个对象
By addButton=By.Id("asdf");
但刚刚意识到我们甚至可以做到-
addButton.FindElement(By.XPath("ABC").
但这并不像 driver.FindElement(addButton)
这样的 return 元素
什么时候使用上面的语句?
以下是使用 Java-binding 中的术语,但它也适用于 C#-Binding:
第一个
@spcial 是对的,Selenium 中没有定义 "By.findElement(By)"。尽管如此,还是有一个 "By.findElement(SearchContext)" 定义,我将在下面解释:
SearchContext 与 By
在 Selenium 中你有一个接口叫做 SearchContext
and then you have the By
class.
一个SearchContext
可以是一个WebElement
或一个WebDriver
现在您有两种查找元素的选项(使用伪代码):
1) SearchContext.findElement(By...)
或
2) By.findElement(SearchContext...)
两者做同样的事情!
假设你有一个驱动程序和这样的 By 变量:
WebDriver driver = new FirefoxDriver();
By addButtonLocator = By.id("asdf");
现在您可以通过两种方式找到您的元素:
1) driver.findElement(addButtonLocator);
或
2) addButtonLocator.findElement(driver);
又来了!两者做同样的事情,这只是 "read" 这些表达式的另一种方式,如下所示:
1) "take the driver and search for an element using this By-statement"
或
2) "take the By-statement and search for an element that fits this statement within driver"
如前所述,如果您使用已识别的元素而不是驱动程序,则可以使用更小的范围。
我对 Selenium 架构有基本的了解,其中 Search Context 是 Web Driver 接口实现的主要接口,由各种浏览器驱动程序扩展 class。通常,我们在 selenium 项目中遵循 POM,并通过 class -
定义每个对象By addButton=By.Id("asdf");
但刚刚意识到我们甚至可以做到-
addButton.FindElement(By.XPath("ABC").
但这并不像 driver.FindElement(addButton)
什么时候使用上面的语句?
以下是使用 Java-binding 中的术语,但它也适用于 C#-Binding:
第一个
@spcial 是对的,Selenium 中没有定义 "By.findElement(By)"。尽管如此,还是有一个 "By.findElement(SearchContext)" 定义,我将在下面解释:
SearchContext 与 By
在 Selenium 中你有一个接口叫做 SearchContext
and then you have the By
class.
一个SearchContext
可以是一个WebElement
或一个WebDriver
现在您有两种查找元素的选项(使用伪代码):
1) SearchContext.findElement(By...)
或
2) By.findElement(SearchContext...)
两者做同样的事情!
假设你有一个驱动程序和这样的 By 变量:
WebDriver driver = new FirefoxDriver();
By addButtonLocator = By.id("asdf");
现在您可以通过两种方式找到您的元素:
1) driver.findElement(addButtonLocator);
或
2) addButtonLocator.findElement(driver);
又来了!两者做同样的事情,这只是 "read" 这些表达式的另一种方式,如下所示:
1) "take the driver and search for an element using this By-statement"
或
2) "take the By-statement and search for an element that fits this statement within driver"
如前所述,如果您使用已识别的元素而不是驱动程序,则可以使用更小的范围。