Selenium in Java Error: Missing or invalid type argument for pointer action

Selenium in Java Error: Missing or invalid type argument for pointer action

我在 Java 中尝试为 Selenium 执行以下代码时收到此错误 "Missing or invalid type argument for pointer action"。

public static void main(String args[]) throws InterruptedException
{
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.flipkart.com/");

    driver.findElement(By.xpath(".//*[@class='_3Ji-EC']/li[8]/a")).click();

    WebElement elem = driver.findElement(By.className("_2zrpKA"));
    elem.sendKeys("ABC");

    WebElement elem2 = driver.findElement(By.xpath(".//*[@class='_2zrpKA 
    _3v41xv']"));
    elem2.sendKeys("XYZ");

    driver.findElement(By.xpath(".//*[@class='_2AkmmA _1LctnI 
    _7UHT_c']")).click();

    System.out.println("Success");

    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Thread.sleep(5000);
    WebElement elem3 = driver.findElement(By.xpath(".//*[@class='_3Ji-
    EC']/li[7]/a"));
    System.out.println("success");
    Actions action = new Actions(driver);
    action.moveToElement(elem3).build().perform();
    driver.findElement(By.xpath(".//*[@class='_1u5ANM']/li[9]/a")).click();
}

我已经使用 Selenium 3.4.0 和 Firefox 51.x、52.x、53.x 以及最新的 geckodriver 16.1 和 16.0 进行了尝试。 当我使用 Firefox 53.x 时,每次出现错误 "Missing or invalid type argument for pointer action".

时都会收到错误 "Expected [object Undefined] undefined to be a string"

在上面的代码中,我能够毫无问题地第二次打印 "success",但之后我收到了错误。

这是您问题的解决方案:

  1. 要使用 Selenium 3.4.0geckodriver v0.16.1 & Mozilla Firefox 53.x,您需要在代码中指定 geckodriver 的绝对路径:

    System.setProperty("webdriver.gecko.driver", "C:\your_directory\geckodriver.exe");
    
  2. 你的 xpath 对我来说似乎有点脆弱。您可能希望构建更独特的逻辑 xpath。要单击 Log In 按钮,您可以执行以下操作:

    driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_2k0gmP'][text()='Log In']")).click();
    
  3. 我建议你不要只依赖class,附加一些更多的属性。 Enter Email 字段的 xpath 可以是:

    WebElement elem = driver.findElement(By.xpath("//input[@class='_2zrpKA']"));
    
  4. Enter Password 的 xpath 不是唯一的,您可能希望将其更改为:

     WebElement elem2 = driver.findElement(By.xpath("//input[@class='_2zrpKA _3v41xv']"));
    
  5. Login 按钮的 xpath 必须是唯一的,如下所示:

    driver.findElement(By.xpath("//button[@class='_2AkmmA _1LctnI _7UHT_c']")).click();
    
  6. 避免使用 Thread.sleep(5000); 而是使用 ImplicitlyWaitExplicitWait

  7. 单击用户名的 xpath 再次存在漏洞,您可能希望将其更改为:

    WebElement elem3 = driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_1AHrFc _2k0gmP']"));
    
  8. 单击 Log Out 按钮的 xpath 再次存在漏洞,您可能希望将其更改为:

    driver.findElement(By.xpath("//div[@class='_1H5F__']/div/ul/li/ul/li/a[@class='_2k0gmP'][text()='Log Out']")).click();
    
  9. 这是您自己的工作代码块,经过一些简单的调整;

    System.setProperty("webdriver.gecko.driver", "C:\your_directory\geckodriver.exe");
    
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.flipkart.com/");
    
    driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_2k0gmP'][text()='Log In']")).click();
    
    //Email
    WebElement elem = driver.findElement(By.xpath("//input[@class='_2zrpKA']"));
    elem.sendKeys("emailid@domain.com");
    
    //Password
    WebElement elem2 = driver.findElement(By.xpath("//input[@class='_2zrpKA _3v41xv']"));
    elem2.sendKeys("pass_word");
    
    //Login Button
    driver.findElement(By.xpath("//button[@class='_2AkmmA _1LctnI _7UHT_c']")).click();
    
    System.out.println("Success");
    
    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Thread.sleep(5000);
    
    //Click on Name
    WebElement elem3 = driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_1AHrFc _2k0gmP']"));
    System.out.println("success");
    Actions action = new Actions(driver);
    action.moveToElement(elem3).build().perform();
    driver.findElement(By.xpath("//div[@class='_1H5F__']/div/ul/li/ul/li/a[@class='_2k0gmP'][text()='Log Out']")).click();
    

如果这回答了您的问题,请告诉我。