Selenium木偶getLocation空指针

Selenium木偶getLocation空指针,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我遇到了: Exception in thread "main" java.lang.NullPointerException at org.openqa.selenium.remote.RemoteWebElement.getLocation(RemoteWebElement.java:338) 尝试在以下位置获取验证码的缓冲区图像时: public buffereImage getCaptchaBufferedImage()引发IOException、InterruptedExceptio

我遇到了:

Exception in thread "main" java.lang.NullPointerException at org.openqa.selenium.remote.RemoteWebElement.getLocation(RemoteWebElement.java:338)
尝试在以下位置获取验证码的缓冲区图像时:

public buffereImage getCaptchaBufferedImage()引发IOException、InterruptedException{
System.out.println(“查找验证码图像”);
this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“hipTemplateContainer”));
System.out.println(“找到的图像”);
WebElement=this.driver.findElement(By.id(“hipTemplateContainer”);
System.out.println(element.getAttribute(“outerHTML”);
List childs=element.findElements(By.xpath(“./*”);
WebElement firstChild=childs.get(0);
System.out.println(firstChild.getAttribute(“outerHTML”);
List childs2=firstChild.findElements(By.xpath(“./*”);
WebElement imageChild=childs2.get(0);
System.out.println(imageChild.getAttribute(“outerHTML”);
((JavascriptExecutor)this.driver).executeScript(“参数[0].ScrollingToView(true);”,imageChild);
字符串id=imageChild.getAttribute(“id”);
this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id));
**Point=firstChild.getLocation()**
byte[]img_bytes=((TakesScreenshot)this.driver).getScreenshotAs(OutputType.bytes);
BuffereImage imageScreen=ImageIO.read(新的ByteArrayInputStream(img_字节));
System.out.println(“下载图像”);
double d=double.parseDouble(firstChild.getCssValue(“height”).split(“px”)[0]);
int高度=(int)d;
double e=double.parseDouble(firstChild.getCssValue(“width”).split(“px”)[0]);
int宽度=(int)e;
BuffereImage captcha=imageScreen.getSubimage(point.getX(),point.getY(),宽度,高度);
JFrame=新JFrame();
frame.getContentPane().setLayout(新的FlowLayout());
frame.getContentPane().add(新的JLabel(新的图像图标(captcha)));
frame.pack();
frame.setVisible(true);
返回验证码;
}

我已经在网上找遍了,找不出这个。。Selenium 3.0中可能存在的错误?如果我跳过获取图像的偏移量,而只是硬编码getSubImage()中的值,…

我尝试了2个多小时,你猜怎么着?我发现了问题

问题: 这是因为
getScreenshotAs
仅获取页面的可见部分(在滚动到验证码后),而没有完成页面,因此导致了所有问题。这导致点(
1041
)返回的Y坐标相对于
完整的
网页,但截图图像相对于部分页面具有不同的验证码(
300
)Y坐标。因此导致以下例外情况:

java.awt.image.RasterFormatException: (y + height) is outside of Raster
X坐标在完整网页和部分网页中的值相同 网页

因此,将Y坐标硬编码为300暂时解决了这个问题。但实际的问题是,为什么不为整个页面截图,而为JU可视页面截图。可能是最新的geckodriver(firefox驱动程序)中的错误。在
Firefox 49版本和Selenium 3版本、geckodriver v0.1.11和Java 1.8中试用

下面是代码。请尝试让我知道:

    driver.get("https://signup.live.com/");
    driver.manage().window().maximize();

    System.out.println("Looking for captcha image");
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("hipTemplateContainer")));
    System.out.println("Found image");

    WebElement element = driver.findElement(By.id("hipTemplateContainer"));
    System.out.println(element.getAttribute("outerHTML"));
    List<WebElement> childs = element.findElements(By.xpath(".//*"));
    WebElement firstChild = childs.get(0);
    System.out.println(firstChild.getAttribute("outerHTML"));
    List<WebElement> childs2 = firstChild.findElements(By.xpath(".//*"));
    WebElement imageChild = childs2.get(0);
    System.out.println(imageChild.getAttribute("outerHTML"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", imageChild);
    String id = imageChild.getAttribute("id");
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));

    Point point = imageChild.getLocation();

    int width = imageChild.getSize().getWidth();
    int height = imageChild.getSize().getHeight();

    System.out.println("height: " + height + "\t weight : " + width);
    System.out.println("X co-ordinate: " + point.getX());
    System.out.println("Y co-ordinate: " + point.getY());

    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    FileUtils.copyFile(screenshot, new File("G:\\naveen\\screenshot.png"));
    BufferedImage imageScreen = ImageIO.read(screenshot);
    System.out.println("Downloaded image");

    BufferedImage captcha = imageScreen.getSubimage(245, 300, width, height);

    ImageIO.write(captcha, "png", screenshot);
    FileUtils.copyFile(screenshot, new File("G:\\naveen\\screenshot1.png"));

    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(new JLabel(new ImageIcon(captcha)));
    frame.pack();
    frame.setVisible(true);
driver.get(“https://signup.live.com/");
driver.manage().window().maximize();
System.out.println(“查找验证码图像”);
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“hipTemplateContainer”));
System.out.println(“找到的图像”);
WebElement=driver.findElement(By.id(“hipTemplateContainer”);
System.out.println(element.getAttribute(“outerHTML”);
List childs=element.findElements(By.xpath(“./*”);
WebElement firstChild=childs.get(0);
System.out.println(firstChild.getAttribute(“outerHTML”);
List childs2=firstChild.findElements(By.xpath(“./*”);
WebElement imageChild=childs2.get(0);
System.out.println(imageChild.getAttribute(“outerHTML”);
((JavascriptExecutor)driver.executeScript(“参数[0]。ScrollingToView(true);”,imageChild);
字符串id=imageChild.getAttribute(“id”);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id));
Point=imageChild.getLocation();
int width=imageChild.getSize().getWidth();
int height=imageChild.getSize().getHeight();
System.out.println(“高度:“+高度+”\t重量:“+宽度”);
System.out.println(“X坐标:+point.getX());
System.out.println(“Y坐标:+point.getY());
文件截图=((TakesScreenshot)driver.getScreenshotAs(OutputType.File);
copyFile(截图,新文件(“G:\\naveen\\screenshot.png”);
BuffereImage imageScreen=ImageIO.read(屏幕截图);
System.out.println(“下载图像”);
BuffereImage captcha=imageScreen.getSubimage(245300,宽度,高度);
ImageIO.write(验证码,“png”,屏幕截图);
copyFile(截图,新文件(“G:\\naveen\\screenshot1.png”);
JFrame=新JFrame();
frame.getContentPane().setLayout(新的FlowLayout());
frame.getContentPane().add(新的JLabel(新的图像图标(captcha)));
frame.pack();
frame.setVisible(true);
以下是已保存的屏幕截图:

  • 只保存可见的网页
  • 子图像-验证码图像:

  • 嘿,纳文,我明天会看一遍,然后发布一个答案:)谢谢你抽出时间!你也认为这是一个错误吗?除非我们都错了,否则你可能会提出一个问题。你的答案是解决另一个问题,那就是文章中描述的问题。它不解释也不解决由
    getLocation
    引发的
    NullPointerException
    问题。通过在第一个Screenshot处循环来验证NullPointer异常,并使用Screenshot.png保存名称(更改绝对路径)。我在本地机器上试过。建议您使用远程Webdriver尝试该代码。geckodriver仅捕获当前在视图端口中可见的页面区域这一事实完全是预期的行为,是不可能的
        driver.get("https://signup.live.com/");
        driver.manage().window().maximize();
    
        System.out.println("Looking for captcha image");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("hipTemplateContainer")));
        System.out.println("Found image");
    
        WebElement element = driver.findElement(By.id("hipTemplateContainer"));
        System.out.println(element.getAttribute("outerHTML"));
        List<WebElement> childs = element.findElements(By.xpath(".//*"));
        WebElement firstChild = childs.get(0);
        System.out.println(firstChild.getAttribute("outerHTML"));
        List<WebElement> childs2 = firstChild.findElements(By.xpath(".//*"));
        WebElement imageChild = childs2.get(0);
        System.out.println(imageChild.getAttribute("outerHTML"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", imageChild);
        String id = imageChild.getAttribute("id");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
    
        Point point = imageChild.getLocation();
    
        int width = imageChild.getSize().getWidth();
        int height = imageChild.getSize().getHeight();
    
        System.out.println("height: " + height + "\t weight : " + width);
        System.out.println("X co-ordinate: " + point.getX());
        System.out.println("Y co-ordinate: " + point.getY());
    
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    
        FileUtils.copyFile(screenshot, new File("G:\\naveen\\screenshot.png"));
        BufferedImage imageScreen = ImageIO.read(screenshot);
        System.out.println("Downloaded image");
    
        BufferedImage captcha = imageScreen.getSubimage(245, 300, width, height);
    
        ImageIO.write(captcha, "png", screenshot);
        FileUtils.copyFile(screenshot, new File("G:\\naveen\\screenshot1.png"));
    
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(captcha)));
        frame.pack();
        frame.setVisible(true);