隱藏瀏覽器
介紹
在使用Selenium采集網(wǎng)頁數(shù)據(jù)時,需要不斷地調(diào)用瀏覽器。實際上,通過對Selenium的設(shè)置,可以達到隱藏瀏覽器的效果。在程序中,對瀏覽器設(shè)置了headless,其作用是實現(xiàn)無界面狀態(tài)。當設(shè)置了隱藏瀏覽器時也是可以正常進行和之前不隱藏瀏覽器一樣的操作的。
FirefoxBinary firefoxBinary = new FirefoxBinary();
// 設(shè)置隱藏瀏覽器模式
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);
示例代碼
使用隱藏瀏覽器模式訪問百度,并嘗試獲取其按鈕的文字信息。通過測試驗證可以在隱藏瀏覽器模式下正常獲取元素值。
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
/**
* 使用Selenium采集網(wǎng)站數(shù)據(jù),隱藏瀏覽器模式
*
* @author zhangyu
* @date 2022/11/18
**/
@Slf4j
public class HeadlessAppExample {
public static void main(String[] args) {
WebDriver driver = null;
try {
System.setProperty("webdriver.gecko.driver", "driver/firefox/geckodriver.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary();
// 設(shè)置隱藏瀏覽器模式
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);
Thread.sleep(1000);
driver.get("https://www.baidu.com");
// 查找某一元素是否存在
WebElement webElement = driver.findElement(By.id("su"));
log.info("元素text信息:{}", webElement.getAttribute("value"));
} catch (Exception e) {
log.error(e.getMessage());
} finally {
assert driver != null;
driver.quit();
}
}
}
【說明】
隱藏瀏覽器資源消耗情況
顯示瀏覽器資源消耗情況
當我們設(shè)置隱藏瀏覽器不代表不啟動瀏覽器,當我們打開任務(wù)管理器可以看到此時firefox是以后臺進程的方式運行的,而且在相同的運行流程下,可以看到顯示瀏覽器資源消耗明顯是大于隱藏瀏覽器的。消耗更少的資源也意味著我們或許可以在同一臺機器上運行更多的流程。但是隱藏瀏覽器可能會出現(xiàn)一些無法預(yù)料的問題,這個需要在開發(fā)中注意下,同時隱藏瀏覽器也無法實時看到運行情況,對于可以使用類似瀏覽器截屏的方式記錄流程視圖。
如果服務(wù)器資源緊張,可以嘗試下這個方式,開發(fā)階段設(shè)置顯示生產(chǎn)環(huán)境設(shè)置隱藏模式。
此外,如果所處理流程涉及隱私數(shù)據(jù),那么此模式也可以作為一種解決方案,通過搭配截屏保存私有服務(wù)器方式,既避免了處理流程時數(shù)據(jù)隱私問題也保存了數(shù)據(jù)處理流程記錄。
瀏覽器截圖
介紹
在網(wǎng)絡(luò)爬蟲中,很多網(wǎng)站會采用驗證碼的方式來反爬蟲,例如在登錄時設(shè)置驗證碼、頻繁訪問時自動彈出驗證碼等。針對模擬登錄時的驗證碼輸入問題,一種簡單的解決方案是將驗證碼保存到本地,之后在程序中手動輸入即可。但對于采集每頁都需要驗證碼的網(wǎng)站來說,則需要使用驗證碼識別算法或調(diào)用一些OCR API來自動識別驗證碼,以保證其效率。
使用Jsoup或者Httpclient,可以直接將驗證碼下載到本地。而對Selenium來說,可以使用截圖的方式截取驗證碼,并保存到本地。然后通過對該截圖進行OCR識別。
不僅對于驗證碼場景適用,對于自動化處理場景,我們也可以通過周期性的瀏覽器頁面截圖做到流程操作記錄留存,對于一些關(guān)鍵性的場景設(shè)置一些截圖可以幫助我們更好的監(jiān)控線上運行情況。尤其是對于發(fā)生異常時,截屏當前頁面,對于排查問題非常的方便。
對于Selenium的截屏可以大致分為兩種,一種是getScreenshotAs(OutputType.FILE);的方式直接對整個瀏覽器頁面進行截圖,另一種是對指定特定HTML元素進行截屏。
示例代碼
對整個瀏覽器頁面進行截屏
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
/**
* 使用Selenium對網(wǎng)頁特定區(qū)域進行截圖并保存本地
*
* @author zhangyu
* @date 2022/11/18
**/
@Slf4j
public class ScreenshotExample {
public static void main(String[] args) {
WebDriver driver = null;
try {
System.setProperty("webdriver.gecko.driver", "driver/firefox/geckodriver.exe");
driver = new FirefoxDriver();
Thread.sleep(1000);
driver.get("https://www.baidu.com");
driver.manage().window().maximize();
// 進行整個屏幕截圖,設(shè)置輸出文件
TakesScreenshot scrShot = ((TakesScreenshot) driver);
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
// 創(chuàng)建本地文件
File DestFile = new File("screenshot", System.currentTimeMillis() + ".png");
FileUtil.copyFile(SrcFile, DestFile);
Thread.sleep(3000);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
assert driver != null;
driver.quit();
}
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-618164.html
指定特定HTML元素進行截屏
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* 使用Selenium對網(wǎng)頁特定區(qū)域進行截圖并保存本地
*
* @author zhangyu
* @date 2022/11/18
**/
@Slf4j
public class Screenshot2Example {
public static void main(String[] args) {
WebDriver driver = null;
try {
System.setProperty("webdriver.gecko.driver", "driver/firefox/geckodriver.exe");
driver = new FirefoxDriver();
Thread.sleep(1000);
driver.get("https://www.baidu.com");
driver.manage().window().maximize();
// 獲取頁面中某個元素
WebElement webElement = driver.findElement(By.id("lg"));
// 進行元素截圖,設(shè)置輸出文件
TakesScreenshot scrShot = ((TakesScreenshot) driver);
File srcFile = scrShot.getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(srcFile);
Point location = webElement.getLocation();
int width = webElement.getSize().getWidth();
int height = webElement.getSize().getHeight();
BufferedImage imageSubimage = bufferedImage.getSubimage(location.getX(), location.getY(), width, height);
ImageIO.write(imageSubimage, "png", new File("screenshot", System.currentTimeMillis() + ".png"));
Thread.sleep(3000);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
assert driver != null;
driver.quit();
}
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-618164.html
到了這里,關(guān)于Selenium隱藏瀏覽器和元素截屏實踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!