最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java+Selenium實(shí)現(xiàn)控制瀏覽器的啟動選項(xiàng)Options

 更新時間:2023年01月11日 10:49:08   作者:洛陽泰山  
這篇文章主要為大家詳細(xì)介紹了如何使用java代碼利用selenium控制瀏覽器的啟動選項(xiàng)Options的代碼操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

簡介

本文主要講解如何使用java代碼利用selenium控制瀏覽器的啟動選項(xiàng)Options的代碼操作教程。

Options選項(xiàng)

這是一個Chrome的參數(shù)對象,在此對象中使用addArgument()方法可以添加啟動參數(shù),添加完畢后可以在初始化Webdriver對象時將此Options對象傳入,則可以實(shí)現(xiàn)以特定參數(shù)啟動Chrome。

設(shè)置瀏覽器后臺運(yùn)行

后臺運(yùn)行瀏覽器,通過selenium取到,洛陽泰山博客的,博主名字。

代碼如下:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        // 設(shè)置后臺靜默模式啟動瀏覽器
        chromeOptions.addArguments("--headless");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
        WebElement element= driver.findElement(By.xpath("http://div[@class='user-profile-head-name']/div[1]"));
         //輸出元素里的文本內(nèi)容
        System.out.println(element.getText());
    }
}

代碼中還提供了另一種后臺運(yùn)行的方法,和上面的效果一樣。

 // 設(shè)置后臺靜默模式啟動瀏覽器
   chromeOptions.setHeadless(true);

設(shè)置瀏覽器最大化

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //設(shè)置瀏覽器啟動最大化(windows寫法)
        chromeOptions.addArguments("start-maximized");
         //mac寫法
        //chromeOptions.addArguments("--start-fullscreen");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
    }
}

自定義瀏覽器大小

     //自定義瀏覽器窗口大小
        chromeOptions.addArguments("--window-size=1366,768");

加載用戶配置

我們在登錄網(wǎng)站的時候,通常需要輸入用戶名、密碼和驗(yàn)證碼,那么有沒有辦法繞過登錄環(huán)節(jié)呢?

有兩種方法可以解決這個問題,一種是利用cookie,一種是利用chrome瀏覽器的用戶配置,這里主要講下利用chrome瀏覽器的用戶配置的實(shí)現(xiàn)。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //加載用戶配置
        chromeOptions.addArguments("--user-data-dir=C:\\Users\\Lenovo\\AppData\\Local\\Google\\Chrome\\User Data");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
    }
}

如何查看User Data的路徑,chrome瀏覽器里輸入chrome://version,即可查看User Data的路徑

隱藏指紋特征

selenium 對于部分網(wǎng)站來說十分強(qiáng)大,但它也不是萬能的,實(shí)際上,selenium 啟動的瀏覽器,有幾十個特征可以被網(wǎng)站檢測到,輕松的識別出你是爬蟲。

不相信?接著往下看,首先你手動打開瀏覽器輸入https://bot.sannysoft.com/,在網(wǎng)絡(luò)無異常的情況下,顯示應(yīng)該如下:

下面通過 selenium 來打開瀏覽器。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        WebDriver driver = new ChromeDriver();
        driver.get("https://bot.sannysoft.com/");
    }
}

通過 webdriver:present 可以看到瀏覽器已經(jīng)識別出了你是爬蟲,我們再試一下無頭瀏覽器。

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
import java.io.File;
import java.io.IOException;
 
 
/**
 * @author tarzan
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //后臺運(yùn)行
        chromeOptions.setHeadless(true);
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://bot.sannysoft.com/");
        Thread.sleep(1000);
        // 截圖操作
        File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        // 截圖存儲
        FileUtils.copyFile(sourceFile, new File("E:\\screenshot\\"+driver.getTitle()+".png"));
    }
}

沒錯,就是這么真實(shí),對于常規(guī)網(wǎng)站可能沒什么反爬,但真正想要抓你還是一抓一個準(zhǔn)的。

說了這么多,是不是 selenium 真的不行?別著急,實(shí)際還是解決方法的。關(guān)鍵點(diǎn)在于如何在瀏覽器檢測之前將這些特征進(jìn)行隱藏,事實(shí)上,前人已經(jīng)為我們鋪好了路,解決這個問題的關(guān)鍵,只需要配置chromeOptions設(shè)置特征隱藏就行。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //禁用WebDriver特征
        chromeOptions.addArguments("--disable-blink-features");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        //隱身模式
        chromeOptions.addArguments("--incognito");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://bot.sannysoft.com/");
    }
}

隱身模式 意思是 Web 瀏覽器上的一個設(shè)置,允許您在瀏覽互聯(lián)網(wǎng)時隱藏起來。隱身模式的工作原理是從 Web 瀏覽會話中刪除本地數(shù)據(jù)。這意味著您的本地搜索歷史記錄中不會記錄任何瀏覽;網(wǎng)站試圖上傳到您計算機(jī)的任何 cookie 都將被刪除或阻止。其他跟蹤程序、臨時文件和第三方工具欄也被禁用。

禁用瀏覽器正在被自動化程序控制的提示

//chrome 76版本以前的寫法
chromeOptions.AddArgument("disable-infobars");
//chrome 76版本以后的寫法
 chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});

模擬移動設(shè)備

//模擬iPhone 6
chromeOptions.addArguments("user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
 //模擬 android QQ瀏覽器
chromeOptions.addArguments("user-agent=\"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");

添加代理

這個地方尤其需要注意的是,在選擇代理時,盡量選擇靜態(tài)IP,才能提升爬取的穩(wěn)定性。因?yàn)槿绻x擇selenium來做爬蟲,說明網(wǎng)站的反爬能力比較高(要不然直接上scrapy了),對網(wǎng)頁之間的連貫性,cookies,用戶狀態(tài)等有較高的監(jiān)測。如果使用動態(tài)匿名IP,每個IP的存活時間是很短的(1~3分鐘)

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        Proxy proxy=new Proxy();
        //示例
        proxy.setHttpProxy("http://D37EPSERV96VT4W2:CERU56DAEB345HU90@proxy.abuyun.com:9020");
        chromeOptions.setProxy(proxy)
        WebDriver driver = new ChromeDriver(chromeOptions);
    }
}

設(shè)置chrome的下載路徑

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
 
 
/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("download.default_directory", "D://download");
        WebDriver driver = new ChromeDriver(chromeOptions);
    }
}

設(shè)置編碼格式

        //設(shè)置瀏覽器編碼為UTF-8
        chromeOptions.addArguments("lang=zh_CN.UTF-8");

到此這篇關(guān)于Java+Selenium實(shí)現(xiàn)控制瀏覽器的啟動選項(xiàng)Options的文章就介紹到這了,更多相關(guān)Java Selenium控制Options內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

临颍县| 武宁县| 云浮市| 山东省| 会东县| 射洪县| 班玛县| 香格里拉县| 衡东县| 平乐县| 白城市| 工布江达县| 樟树市| 兴城市| 苏州市| 石屏县| 东莞市| 綦江县| 蓝山县| 胶南市| 体育| 昌图县| 辽源市| 乌拉特前旗| 大理市| 宿迁市| 乌审旗| 保山市| 濮阳市| 罗城| 苍梧县| 岗巴县| 新郑市| 宝坻区| 天等县| 寿宁县| 湘潭市| 平谷区| 临澧县| 都江堰市| 安康市|