0

i am trying to capture the captcha image from a web page using the following code

WebElement captchaEl = driver.findElement(By.id("captchaLogin"));
//System.out.println("Captcha: "+ source);
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
Point point = captchaEl.getLocation();
int eleX = point.getX();
int eleY = point.getY();
int eleWidth = captchaEl.getSize().getWidth();
int eleHeight = captchaEl.getSize().getHeight();
BufferedImage eleScreenshot= fullImg.getSubimage(eleX, eleY, eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
FileUtils.copyFile(screenshot, new File(outputFileName));

It ends up capturing some other portion in the page, rather than the captcha image. Although it works if phantomjs driver is used, but doesn't work if chromedriver or geckodriver(firefoxdriver) is used. What could be the problem here?

chenzen
  • 63
  • 1
  • 1
  • 7
  • try to put some focus over the capcha – Shubham Jain Aug 30 '17 at 12:06
  • @ShubhamJain how? by clicking on it first? – chenzen Aug 30 '17 at 12:07
  • Action classes .. https://stackoverflow.com/questions/11337353/correct-way-to-focus-an-element-in-selenium-webdriver-using-java – Shubham Jain Aug 30 '17 at 12:11
  • @ShubhamJain unfortunately that doesn't work either. i added new Actions(driver).moveToElement(captchaEl).click().perform(); to top it, i added Thread.sleep(1000); so that it waits for 1 more sec. – chenzen Aug 30 '17 at 12:24
  • Might try this https://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-a-specific-element-rather-than-entire-page-usin – smit9234 Aug 30 '17 at 14:32
  • @smit9234 i am practically using the same code already. the problem is with captchaEl.getLocation() giving a incorrect location of captchaEl. – chenzen Aug 30 '17 at 15:36

0 Answers0