简体   繁体   English

使用Java的Selenium Webdriver

[英]Selenium Webdriver using java

Why is the New keyword not needed when instantiating the WebElement class but not for the Select class in Selenium ? 为什么在实例化WebElement类时不需要New关键字,而在Selenium中选择Select类却不需要呢? For WebElement: 对于WebElement:

WebElement username = driver.findElement(By.id(“Email”));

But for Select: 但对于Select:

Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));

why the New keyword in Select and vice versa? 为什么在Select中使用New关键字,反之亦然?

Because WebElement IS NOT a class it's an Interface reside in org.openqa.selenium which extends SearchContext and Select is Class 因为WebElement 不是类,所以它是一个接口,驻留在org.openqa.selenium ,该接口扩展了SearchContext并且SelectClass

More info: You do not instantiate an Interface, just implementing them will have result. 更多信息:您无需实例化接口,只需实现它们就会有结果。 this will help you to understand 将帮助您了解

Cause findElement method itself returns WebElement object. 原因findElement方法本身返回WebElement对象。 See definition of By.class . 请参见By.class的定义。 And WebElement is an interface so you can't instantiate it. WebElement是一个接口,因此您无法实例化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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