Selenium Webdriver intercept site verification code

Source: Internet
Author: User
Tags gettext gety

When doing the crawler project, sometimes encounter the problem of verification code, because some site verification code is dynamically generated, even if the same link, at different times access may produce different verification code,

At the beginning of the idea is to open the link to the verification code, and then through the Java code GET request to save the verification code picture to local, and then use the Code tool to parse the verification code, the verification code automatically entered the verification box

Can be the verification code problem solved, but the problem, each request the same address, the resulting code image is not the same, so this method does not work. So you can only use the picture first selenium Webdriver

Intercept to local, and then use the Code tool to parse OK, automatically fill in the verification, very good to solve the problem of the verification code.

Package Com.entrym.main;import Java.awt.image.bufferedimage;import Java.io.file;import java.io.IOException;import Java.util.arraylist;import java.util.date;import java.util.hashmap;import Java.util.list;import java.util.Set; Import Javax.imageio.imageio;import Org.apache.commons.io.fileutils;import org.apache.commons.lang3.StringUtils; Import Org.json.jsonobject;import org.jsoup.jsoup;import org.jsoup.nodes.document;import org.jsoup.nodes.Element; Import Org.openqa.selenium.by;import Org.openqa.selenium.cookie;import Org.openqa.selenium.outputtype;import Org.openqa.selenium.point;import Org.openqa.selenium.takesscreenshot;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.chrome.chromedriver;import Org.openqa.selenium.support.ui.expectedcondition;import Org.openqa.selenium.support.ui.webdriverwait;import Com.entrym.crawler.util.verifycode.captcha;import Com.entrym.crawler.util.verifycode.damautil;import Com.entrym.domain.sogouinfo;importCom.entrym.domain.wxinfo;import Com.entrym.util.configutil;import Com.entrym.util.dateutil;import Com.entrym.util.httputils;import Com.google.gson.gson;import Com.vdurmont.emoji.emojiparser;public class WebTest {        private static final String get_title= "/titles/getxiaoshuo"; private static final string path=new File ("Config/config.properties"). GetAbsolutePath ();p rivate static final string Chrome_home=new File ("Config/chromedriver.exe"). GetAbsolutePath ();p rivate static final String chrome_home_linux=new File ("Config/chromedriver"). GetAbsolutePath ();p rivate static final String baseurl=configutil.reads (PATH, "BASEURL") ;p ublic static void Main (string[] args) throws IOException {Webdriver Driver=null;//system.setproperty (" Webdriver.gecko.driver ", firefox_home); System.out.println (PATH); String osname=system.getproperty ("Os.name"). toLowerCase (); if (Osname.indexof ("Linux") >=0) {System.setproperty ( "Webdriver.chrome.driver", chrome_home_linux);//driver = new Marionettedriver ();} Else{system.sEtproperty ("Webdriver.chrome.driver", chrome_home);//driver = new Marionettedriver (); Driver=new chromedriver ();d river.get ("Http://weixin.sogou.com/antispider/?from=%2fweixin%3Ftype%3d2%26query%3dz +%26ie%3dutf8%26s_from%3dinput%26_sug_%3dy%26_sug_type_%3d "); Webelement ele = driver.findelement (By.id ("Seccodeimage"));//Get entire page Screenshotfile screenshot = (( Takesscreenshot) driver). Getscreenshotas (Outputtype.file); BufferedImage fullimg = imageio.read (screenshot);//Get the location of element in the pagepoint point = Ele.getlocation ( );//Get width and height of the elementint elewidth = Ele.getsize (). getwidth (); int eleheight = Ele.getsize (). GetHeight (); Crop the entire page screenshot to get only element screenshotbufferedimage elescreenshot= fullimg.getsubimage (point.ge  TX (), Point.gety (), Elewidth, Eleheight) imageio.write (elescreenshot, "PNG", screenshot);//Copy The element screenshot to Diskfile screenshotlocation = new File ("D:/captcha/test.png"); Fileutils.copyfile (screEnshot, screenshotlocation); Webelement classelement = driver.findelement (By.classname ("P2")); String Errortext=classelement.gettext (); System.out.println ("The content of the output is" +classelement.gettext ()), if (Errortext.indexof ("User Hello, your access is too frequent to confirm this access for normal user behavior") >=0            {System.out.println ("*********************");D amautil util=new damautil ();            System.out.println ("===================");           String code= ""; Verification Code CAPTCHA captcha=new Captcha (); Captcha.setfilepath ("test.png"); code = Damautil.getcaptcharesult (CAPTCHA); SYSTEM.OUT.PRINTLN ("Code is processed out of the verification code is" +code);        Webelement elementsumbit = driver.findelement (By.id ("Seccodeinput")); Enter the keyword Elementsumbit.sendkeys (code), try {thread.sleep (),} catch (Interruptedexception e) {//TODO auto-generated        Catch Blocke.printstacktrace ();} Submit the Formelementsumbit.submit () where input is located;          SYSTEM.OUT.PRINTLN ("Success"); }}}

  

The above code, the key code in the Stack overflow get, have to say that Google is still very powerful

Driver.get ("http://www.google.com"); Webelement Ele= Driver.findelement (By.id ("Hplogo"));//Get entire page screenshotFile screenshot =((takesscreenshot) driver). Getscreenshotas (Outputtype.file); BufferedImage fullimg=Imageio.read (screenshot);//Get The location of an element on the pagePoint point =ele.getlocation ();//Get width and height of the elementintElewidth =ele.getsize (). getwidth ();intEleheight =ele.getsize (). GetHeight ();//Crop the entire page screenshot to get only element screenshotBufferedImage elescreenshot=Fullimg.getsubimage (Point.getx (), Point.gety (), Elewidth, eleheight); Imageio.write (Elescreenshot,"PNG", screenshot);//Copy The element screenshot to diskFile screenshotlocation =NewFile ("C:\\images\\googlelogo_screenshot.png"); Fileutils.copyfile (screenshot, screenshotlocation);
View Code
以上就是关键的截取代码,在国外的链接是http://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-a-specific-element-rather-than-entire-page-usin
感兴趣的小伙伴可以研究一下

Selenium Webdriver intercept site verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.