编译错误:线程“main”java.lang中出现异常错误:使用selenium webdriver的findelement(by.id())存在未解决的编译问题

rjee0c15  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(297)

我已经将java、eclipse和selenium配置为用于自动化基于web的应用程序。但是,我面临以下提到的问题:
问题1:警告:构建路径指定执行环境javase-14。工作区中没有安装与此环境严格兼容的JRE。
问题2:无法访问代码中的selenium对象。
问题3:获取下面提到的编译错误:线程“main”java.lang中出现异常。错误:未解决的编译问题:类型对象的方法sendkeys(string)未定义类型对象的方法sendkeys(string)未定义
代码如下:

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();
xiozqbni

xiozqbni1#

此错误消息。。。

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

…意味着方法 sendKeys(String) 对于类型对象未定义 By.Id .
根据文件,方法是 By id​(java.lang.String id) 不是 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.

解决方案

因此,您的代码行将是:

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

efzxgjgh2#

InternetExplorerDriver driver

更改为

WebDriver driver

当您将驱动程序定义为internetexplorerdriver时,您正在向下转换它,这意味着internetexplorerdriver的本机方法将仅可用
您必须向上转换webdriver接口方法才能使用。
定义webdriver时,webdriver方法和internetexplorerdriver方法都可用

相关问题