2017-05-14 34 views
0

在Java中尝试执行Selenium的以下代码时,出现此错误“缺少或无效的指针操作类型参数”。Java中的Selenium错误:缺少或无效的指针操作类型参数

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时,出现错误“Expected [object Undefined] undefined undefined to a string”else每当我收到错误时“指针操作缺少或无效的类型参数”。

在上面的代码中,我能够在没有任何问题的情况下第二次打印“成功”,但之后出现错误。

+0

哪一行给出错误? –

+0

最后3行。我可以看到在我的控制台上打印的最后四行中写的“成功”。 –

+0

对我来说很好用铬58.x镀铬汽油2.28和硒3.4。0 – kushal

回答

0

这里是解决你的问题:

  1. 要与Selenium 3.4.0工作与geckodriver 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. 我建议你不要只依靠一流的,有些附加更多的属性。为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. 中的XPath点击Log Out按钮又是脆弱的,你可能会喜欢将其更改为:

    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("[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(); 
    

让我知道如果这个回答你的问题。

+0

非常感谢。这有助于我现在能够在Firefox中执行代码。 并感谢您的建议。我会在下次记住他们。 –

+0

@RiteshGupta好消息:)很高兴我能够提供帮助。 – DebanjanB

相关问题