在打开Gmail时,出现以下错误 - 预期[object Undefined]未定义为字符串

时间:2017-05-15 16:36:53

标签: selenium selenium-webdriver

Expected [object Undefined] undefined to be a string, 
The code I am using is following:                 
System.setProperty("webdriver.gecko.driver","E:\\Software\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
//wd.findElement(By.xpath(".//*
[@id='gbw']/div/div/div[1]/div[1]/a")).click();
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[@id='identifierId']"));
e1.sendKeys("abc@gmail.com");
wd.findElement(By.xpath("//div[@id='identifierNext']/content/span[text()='Ne
xt']")).click();

错误日志

错误日志 线程“main”中的异常java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkState(ZLjava / lang / String; Ljava / lang / Object;)V     在org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)     在org.openqa.selenium.firefox.GeckoDriverService.access $ 000(GeckoDriverService.java:41)     在org.openqa.selenium.firefox.GeckoDriverService $ Builder.usingFirefoxBinary(GeckoDriverService.java:108)     在org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)     在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:108)     在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:104)     在register_prctc.gmail.main(gmail.java:15)

2 个答案:

答案 0 :(得分:0)

以下是您的问题的解决方案:

  1. 使用Selenium 3.4.0,geckodriver v0.16.1&最新的Mozilla Firefox 53.x需要在代码中设置geckodriver的绝对路径: System.setProperty("webdriver.gecko.driver","C:\\your_dir\\geckodriver.exe");
  2. 根据最佳做法,您不应使用Thread.sleep(6000),而应使用ImplicitlyWaitExplicitWait

  3. 您使用的xpath .//[@id='gbw']/div/div/div[1]/div[1]/a未标识任何唯一元素。要查找元素Gmail链接,您可以将linkText定位符用作:

    wd.findElement(By.linkText("Gmail")).click();
    
  4. 要将文字发送到Email or Phone字段,请提供唯一的xpath

    WebElement e1= wd.findElement(By.xpath("//input[@id='identifierId']"));
    
  5. 点击Next按钮的xpath看起来容易受到攻击,您可能希望将其更改为:wd.findElement(By.xpath("//div[@id='identifierNext']/content/span[text()='Next']")).click();

  6. 以下是您自己的代码的工作集,并进行了一些简单的调整:

    System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
    WebDriver wd= new FirefoxDriver();
    wd.get("https://www.google.co.in/");
    wd.findElement(By.linkText("Gmail")).click();
    WebElement e1= wd.findElement(By.xpath("//input[@id='identifierId']"));
    e1.sendKeys("id@gmail.com");
    wd.findElement(By.xpath("//div[@id='identifierNext']/content/span[text()='Next']")).click();
    
  7. 如果这回答你的问题,请告诉我。

答案 1 :(得分:0)

删除Selenium-java-2.53.1.jar文件并更新所有jars

enter image description here