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

1.6k Views Asked by At

I am getting this error "Missing or invalid type argument for pointer action" while trying to execute the below code for Selenium in Java.

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

I have tried this using Selenium 3.4.0 and Firefox 51.x, 52.x, 53.x with the latest geckodriver 16.1 and 16.0 . When I used Firefox 53.x, I was getting the error "Expected [object Undefined] undefined to be a string" else every time I am getting the error "Missing or invalid type argument for pointer action".

In the above code I am able to get "success" printed 2nd time without any problem but after that I am getting the error.

1

There are 1 best solutions below

2
On BEST ANSWER

Here is the solution to your Question:

  1. To work with Selenium 3.4.0 with geckodriver v0.16.1 & Mozilla Firefox 53.x you need to specify the absolute path of the geckodriver in your code as:

    System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
    
  2. Your xpath seems petty vulnerable to me. You may want to construct more unique logical xpath. To click on Log In button you may do:

    driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_2k0gmP'][text()='Log In']")).click();
    
  3. I would suggest you not to rely only on class, append some more properties. The xpath for Enter Email field can be:

    WebElement elem = driver.findElement(By.xpath("//input[@class='_2zrpKA']"));
    
  4. The xpath for Enter Password is not unique, you may like to change it to:

     WebElement elem2 = driver.findElement(By.xpath("//input[@class='_2zrpKA _3v41xv']"));
    
  5. The xpath for the Login button needs to be unique as follows:

    driver.findElement(By.xpath("//button[@class='_2AkmmA _1LctnI _7UHT_c']")).click();
    
  6. Avoid using Thread.sleep(5000); rather use ImplicitlyWait or ExplicitWait

  7. The xpath to click on the Username is again vulnerable, you may like to change it to:

    WebElement elem3 = driver.findElement(By.xpath("//div[@class='AsXM8z']/ul/li/a[@class='_1AHrFc _2k0gmP']"));
    
  8. The xpath to click the Log Out button is again vulnerable, you may like to change it to:

    driver.findElement(By.xpath("//div[@class='_1H5F__']/div/ul/li/ul/li/a[@class='_2k0gmP'][text()='Log Out']")).click();
    
  9. Here is your own working code block with some simple tweaks;

    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("[email protected]");
    
    //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();
    

Let me know if this Answers your Question.