问题
I have configured Java, Eclipse and Selenium to be used for automating a web based application. But, I am facing below mentioned issues:
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.
Issue 2: Unable to access objects of selenium in the code.
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
Below is the code:
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();
回答1:
This error message...
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
...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");
回答2:
InternetExplorerDriver driver
Change to
WebDriver driver
When you define driver as internetexploterdriver you are downcasting it meaning methods native to internetexplorerdriver will only be available
You have to upcast for the WebDriver interface methods to be available.
When you define WebDriver driver , both WebDriver methods and internetexplorerdriver methods will be available
来源:https://stackoverflow.com/questions/65140315/compilation-error-exception-in-thread-main-java-lang-error-unresolved-compil