使用Selenium WebDriver的sendKeys()时出错。无法解析Java.lang.CharSequence
我导入了以下内容,但在使用sendKeys();
时仍然出错sendKeys();
import org.openqa.selenium.*; import org.openqa.selenium.firefox.*; import org.testng.Assert; import org.openqa.selenium.WebDriver;
注意:我在Eclipse中使用Selenium WebDriver。
示例代码如下。
import org.openqa.selenium.*; import org.openqa.selenium.firefox.*; import org.testng.Assert; import org.openqa.selenium.WebDriver; public class Practice { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); String baseUrl = "http://www.facebook.com"; String tagName=""; driver.get(baseUrl); tagName = driver.findElement(By.id("email")).getTagName(); System.out.println("TagName: "+tagName); WebElement myElement = driver.findElement(By.id("username")); myElement.sendKeys("text"); } }
我收到一条错误说明
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
指向myElement.sendKeys("text");
你们其中一个人能告诉我这里有什么不对吗
你可以试试这个,类似的问题已在这里得到解答#sendKeys Issue
myElement .sendKeys(new String[] { "text" }); //You could create a string array
或简单地说
myElement .sendKeys(new String { "text" });
它需要升级编译器合规性。 对于Eclipse:请按照以下步骤操作:
- 右键单击您的java项目并选择Build Path – >单击Configure Build Path …
- 在项目属性窗口中:单击左侧面板中的/选择Java Compiler
- 在右侧面板中:将编译器合规性级别从1.4更改为1.7或更高
- 最后点击Apply和OK
我在使用eclipse Kepler时遇到了同样的问题。
问题域:
我的java 编译器合规级别是1.4
解:
所以使用构建路径>> configure build path >> java compiler >>将编译器合规性级别更改为1.7或更高
这可以解决问题。
这是因为您的eclipse编辑器。
请按照以下步骤克服该错误。
- 右键单击java Project。
- 选择构建路径>配置构建路径。
这里有两件事你需要检查
A.Java Build Path> Libraries-这里编辑器应该参考你机器上安装的版本。 如果它引用了旧库文件,则将其删除并单击“添加库”,然后从“列表”中选择最新的Jre系统库。
B.Java编译器。 此编译器合规性级别应该是您计算机上安装的最新级别。
在使用JDK8时,您的编译器版本应为1.8,只有最新的eclipse版本才能获得 – ECLIPSE LUNA。 替代方法是将JDK8降级为JDK7,它仍然可以工作。
检查Eclipse版Eclipse 4.3(Kepler)及以上版本需要升级JAVA JDK 1.8。
安装后只需检查Eclipse:构建路径>> configure build path >> java compiler >>改变了编译器的合规级别是1.8
注意: 1。我建议使用Java JDK 1.8版本和Eclipse Eclipse 4.6(Neon)2。Selenium 3+版本jar文件仅支持JAVA JDK 1.8及以上版本。
面对同样的问题。
升级到Eclipse Luna然后将编译器版本更改为1.8时问题得到解决
编译器版本1.8不适用于以前的版本