编译错误:线程&"main&"中的异常;java.lang.Error:使用Selenium Webdriver的findElement(By.Id())无法解决的编译问题

编译错误:线程&

问题描述:

我已经将Java,Eclipse和Selenium配置为用于自动化基于Web的应用程序.但是,我面临以下提到的问题:

I have configured Java, Eclipse and Selenium to be used for automating a web based application. But, I am facing below mentioned issues:

问题1:警告:构建路径指定执行环境JavaSE-14.在工作区中没有安装与该环境严格兼容的JRE.

Issue 1: Warning : Build path specifies execution environment JavaSE-14. There are no JREs installed in the workspace that are strictly compatible with this environment.

问题2:无法访问代码中的硒对象.

Issue 2: Unable to access objects of selenium in the code.

问题3:收到以下提到的编译错误:线程"main"中的异常java.lang.Error:未解决的编译问题:未为对象类型定义方法sendKeys(String)对于对象类型,未定义方法sendKeys(String)

Issue 3: Getting the below mentioned compilation error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method sendKeys(String) is undefined for the type Object The method sendKeys(String) is undefined for the type Object

下面是代码:

package palettepkg;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class palettelogin {

    public static void main(String[] args) {
        
        System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Selenium\\Drivers\\IEDriverServer.exe");
        
        InternetExplorerDriver driver = new InternetExplorerDriver();
        
        driver.get("http://adclnapdev01v.bcg.com:8030");
        
        //driver.manage().window().maximize();
        
        driver.findElement(By.Id("unamebean")).sendKeys("VERMA");
        
        Thread.sleep(2000);
        
        driver.findElement(By.className(".LoginText")).sendKeys("Work@12345678");
        
        //driver.close();

此错误消息...

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method sendKeys(String) is undefined for the type Object The method sendKeys(String) is undefined for the type Object

...表示对于对象 By.Id 类型的类型,未定义方法 sendKeys(String).

...implies that the the method sendKeys(String) is undefined for the type Object By.Id.

根据文档,该方法为

As per the documentation the method is By id​(java.lang.String id) not By.Id:

public static By id​(java.lang.String id)

Parameters:
id - The value of the "id" attribute to search for.

Returns:
A By which locates elements by the value of the "id" attribute.


解决方案

如此有效,您的代码行将是:


Solution

So effectively, your line of code will be:

driver.findElement(By.id("unamebean")).sendKeys("VERMA");