-3

I have code made in selenium webdriver Java using HTML Unit driver I am inputting various fields in that program.

My question is Which language should I use in order to get captcha displayed on the web page opened by HTML unit driver.what I want is that a window should pop up to the user, on this window captcha should be displayed which was opened by html unit driver also this window will also have a text field to input that captcha also, this input field will be linked with webpage's input field of captcha which is currently being processed by html unit driver and once the captcha has been entered by the user rest of the selenium code should be executed.

Initially, I was thinking of using jsoup to get captcha and display it to user but that is not working because both selenium and jsoup are picking different captchas as they start a new connection.

My earlier code was like this

System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("URL abc"); 

Connection conn =  Jsoup.connect("URL abc");
Document d = conn.get();


Element captcha = d.select("#cimage").first();
if (captcha == null) {
    throw new RuntimeException("Unable to find captcha...");
}

// Fetch the captcha image
Connection.Response response = Jsoup //
        .connect(captcha.absUrl("src")) // Extract image absolute URL
        .cookies(conn.response().cookies()) // Grab cookies
        .ignoreContentType(true) // Needed for fetching image
        .execute();

// Load image from Jsoup response
ImageIcon image = new ImageIcon(ImageIO.read(new ByteArrayInputStream(response.bodyAsBytes())));
// Show image
JOptionPane.showMessageDialog(null, image, "Captcha image", JOptionPane.PLAIN_MESSAGE);

Using chrome driver I am getting two different captchas probably because both selenium and jsoup are starting a new connection Below is the image for the same

Snapshot

Any suggestion or examples would be immensely appreciated. Please let me know if any additional information is needed!

Ory Zaidenvorm
  • 896
  • 1
  • 6
  • 20
Ajay
  • 167
  • 4
  • 15

3 Answers3

4

You shouldn't mix Selenium driver and Jsoup here. The ChromeDriver has downloaded the page with the captcha image. So it's the ChromeDriver responsibility to give the image.

You can acheive this in two steps:

  1. Take a screenshot
  2. Extract the subimage of the previous screenshot corresponding to the captcha

Here is an example (tidied) originally posted on Selenium Users Google group :

DownloadImage ( //
     By.xpath("//*[@id='content']/div/div[1]/div/div/div[1]/img"), //
     "D:\\Download\\image.png");

public void DownloadImage(By by,String loc) throws IOException {
    WebElement Image=driver.findElement(by);
    int width=Image.getSize().getWidth();
    int height=Image.getSize().getHeight();

    File screen=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    BufferedImage img=ImageIO.read(screen);

    BufferedImage dest=img.getSubimage(Image.getLocation().getX(), Image.getLocation().getY(), width, height);
    ImageIO.write(dest, "png", screen);
    File file=new File(loc);
    FileUtils.copyFile(screen,file);
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
1

Have you considered using a pop up just to let the user know that the captcha needs to be filled in (without the actual image of the captcha displayed)?

User can go and enter the captcha value in the browser and then close the pop up. Selnium can wait until the user closes the pop up before continuing.

If the user has no access to the browser (e.g. running headless or on another machine) you could snapshot the captcha image and display it to the user (or save it in a shared location for the user to open).

If you want to crop the snapshot to just the captcha you will have to write a little bit more code to do so

Ory Zaidenvorm
  • 896
  • 1
  • 6
  • 20
  • Thank you so much for such a detailed response. Although in the above code I have used chrome driver but my ultimate goal is to use HTMLUnit driver and display that captcha as a pop up to the user and ask him to input the captcha that way execution will also be fast. The problem is that I have to enter the captcha on real time basis coz as soon as I enter the correct captcha after that rest of the execution will be done. Basically, the scenario is selenium opens the website inputs username nd password then it should wait for user to enter captcha and then click on login button. – Ajay Mar 24 '16 at 03:58
  • I am not sure how to show the captcha to the user on real-time basis and ask him to enter it. I think selenium alone might not be able to do this. we might need help of some other prog lang like jsoup or something – Ajay Mar 24 '16 at 03:59
  • So just to be clear your exact scenario is that user has access to the machine but the code is running headless? I'm sure you can overcome that with Java - let me know and I'll try and help further. – Ory Zaidenvorm Mar 24 '16 at 06:20
  • Yes, that's correct. Selenium code will run that will open the browser input username and password but for captcha will have to be entered by the user manually. As this script finally has to run on HtmlUnit Driver so I am not able to figure out how to get input for captcha from the user while the script is running. – Ajay Mar 24 '16 at 14:13
0

Most of the companies use captchas in the user registration page of their sites. The purpose of captcha on any webpage is to differentiate human from machine, so as prevent hacking,and protect their websites against bots. So these pages can't be automated fully. In-fact Captcha itself is implemented to prevent automation. Hence can not be automated and not breakable. If any automation program tries to hack any website that company may proceed for legal actions against the hacker. So better do not automate such webpages which have Captcha! Hope this answers your question. Thanks

karusai
  • 126
  • 1
  • 1
  • 9
  • I am not hacking any webpage the captcha that would be displayed on the webpage should be shown to user so that he can manually enter it. – Ajay Mar 24 '16 at 01:17
  • got your point! is it opening the window pop up to the user, where captcha about to be displayed, using other browsers other than HTML unit driver. – karusai Mar 24 '16 at 01:31
  • Yes I have added screenshot for your reference. – Ajay Mar 24 '16 at 01:38
  • @Ajay: I understand your situation, We are not working as a bot but as an live agent of the real user who is solving the challenges as a real human. Now the real challenge starts when you are pose with image based captcha – Talk is Cheap Show me Code Oct 23 '18 at 07:08