如何在java中使用selenium爬取圖片簽名

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在java中使用selenium爬取圖片簽名,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供岐山網(wǎng)站建設(shè)、岐山做網(wǎng)站、岐山網(wǎng)站設(shè)計(jì)、岐山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、岐山企業(yè)網(wǎng)站模板建站服務(wù),十余年岐山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

1.注意

對(duì)應(yīng)的版本非常重要,使用selenium得下載與游覽器版本相對(duì)應(yīng)的插件,有火狐和谷歌我用的谷歌,貼下谷歌driver的插件

查看谷歌版本:

如何在java中使用selenium爬取圖片簽名

2.插件存放路徑

如何在java中使用selenium爬取圖片簽名

3.獲取簽名圖片存放路徑

如何在java中使用selenium爬取圖片簽名

4.Controller代碼如下

 @ResponseBody
 @RequestMapping(value = "signatureGenerationv")
 public String signatureGeneration(String userName, HttpServletRequest request) throws Exception{
 System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
 //初始化一個(gè)谷歌瀏覽器實(shí)例,實(shí)例名稱(chēng)叫driver
 WebDriver driver = new ChromeDriver();
 // get()打開(kāi)一個(gè)站點(diǎn)
 driver.get("https://www.yishuzi.cn/qianming/");
 //簽名
 driver.findElement(By.xpath("//*[@id=\"text\"]")).clear();
 driver.findElement(By.xpath("//*[@id=\"text\"]")).sendKeys(userName);
 //簽名配色
 driver.findElement(By.xpath("//*[@id=\"index\"]/div[1]/div/div[4]/div[2]/div/div[7]/div/div/p[2]/a[2]")).click();
 //最大化窗口
 driver.manage().window().maximize();
 Thread.sleep(2000);
 WebElement webElement = driver.findElement(By.xpath("//*[@id=\"SaveImg\"]"));
 //生成簽名圖片,裁剪圖片
 byte[] imageData = BrowserUtil.captureElement(webElement);
 //設(shè)置隱性等待時(shí)間
 driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
 //getTitle()獲取當(dāng)前頁(yè)面title的值
 System.out.println("當(dāng)前打開(kāi)頁(yè)面的標(biāo)題是: "+ driver.getTitle());
 //關(guān)閉并退出瀏覽器
 driver.quit();
 //把圖片轉(zhuǎn)換成能夠在頁(yè)面直接展示的BASE64Encoder格式
 BASE64Encoder base64Encoder = new BASE64Encoder();
 String img = base64Encoder.encode(imageData);
 return img;
 }

5.BrowserUtil工具類(lèi)代碼

public static byte[] captureElement(WebElement element) throws Exception {
 WrapsDriver wrapsDriver = (WrapsDriver) element;
 // 截圖整個(gè)頁(yè)面
 byte[] screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.BYTES);

 ByteArrayOutputStream out = new ByteArrayOutputStream();
 try {
  BufferedImage img = ImageIO.read(new ByteArrayInputStream(screen));
  ImageIO.write(img, "png", new File("img/imgs/img"+System.currentTimeMillis()+".png"));

  // 獲得元素的高度和寬度
  int width = element.getSize().getWidth();
  int height = element.getSize().getHeight();
  // 創(chuàng)建一個(gè)矩形使用上面的高度,和寬度
  java.awt.Rectangle rect = new java.awt.Rectangle(width, height);
  // 得到元素的坐標(biāo)
  Point p = element.getLocation();

  float rate = img.getWidth()/1280;
  BufferedImage dest = img.getSubimage(
   (int)(p.getX()*rate),
   (int)(p.getY()*rate),
   (int)(rect.width*rate),(int)(rect.height*rate));
  ImageIO.write(dest, "png", new File("img/"+System.currentTimeMillis()+".png"));
  boolean flag = ImageIO.write(dest, "png", out);
  byte[] imageData = out.toByteArray();
  return imageData;
 } finally {
  out.close();
 }
 }

6.index.html代碼

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>簽名生成</title>
 <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
<div>
 請(qǐng)輸入名字:<input type="text" id="userName" name="userName"><input type="button" id="btn" value="生成">
 <!--<img id="img" src="">-->
 <div id="img"></div>
</div>
<script>

 $("#btn").click(function () {
  var userName = $("#userName").val();
  $.ajax({
  url : "signatureGenerationv",
  type : "post",
  // dataType :"string",
  data :{'userName':userName},
  success : function(data) {
   data = data.replace(/\\n/g,'\n')//去換行
   var str = data.substring(0,data.length)
   var base = "data:image/png;base64,"+str;
   var img = new Image();//創(chuàng)建img容器
   img.src = base;
   document.body.appendChild(img);
  },
  error : function(data) {
   alert("失敗"+data.responseText)
  }
  })
 })

</script>
</body>
</html>

7.結(jié)果圖

如何在java中使用selenium爬取圖片簽名

8.pom包

<!--selenium依賴(lài)文件-->
 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.8.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-server</artifactId>
  <version>3.8.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-chrome-driver</artifactId>
  <version>3.8.1</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-chrome-driver</artifactId>
  <version>3.8.1</version>
 </dependency>
 <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-htmlunit-driver</artifactId>
  <version>2.52.0</version>
 </dependency>
 <dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
 </dependency>

Java可以用來(lái)干什么

Java主要應(yīng)用于:1. web開(kāi)發(fā);2. Android開(kāi)發(fā);3. 客戶(hù)端開(kāi)發(fā);4. 網(wǎng)頁(yè)開(kāi)發(fā);5. 企業(yè)級(jí)應(yīng)用開(kāi)發(fā);6. Java大數(shù)據(jù)開(kāi)發(fā);7.游戲開(kāi)發(fā)等。

上述就是小編為大家分享的如何在java中使用selenium爬取圖片簽名了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:如何在java中使用selenium爬取圖片簽名
鏈接分享:http://muchs.cn/article36/gpphsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、靜態(tài)網(wǎng)站、網(wǎng)站改版、云服務(wù)器網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站