Java+Selenium實現(xiàn)控制瀏覽器的啟動選項Options
簡介
本文主要講解如何使用java代碼利用selenium控制瀏覽器的啟動選項Options的代碼操作教程。
Options選項
這是一個Chrome的參數(shù)對象,在此對象中使用addArgument()方法可以添加啟動參數(shù),添加完畢后可以在初始化Webdriver對象時將此Options對象傳入,則可以實現(xiàn)以特定參數(shù)啟動Chrome。
設(shè)置瀏覽器后臺運行
后臺運行瀏覽器,通過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()); } }
代碼中還提供了另一種后臺運行的方法,和上面的效果一樣。
// 設(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)站的時候,通常需要輸入用戶名、密碼和驗證碼,那么有沒有辦法繞過登錄環(huán)節(jié)呢?
有兩種方法可以解決這個問題,一種是利用cookie,一種是利用chrome瀏覽器的用戶配置,這里主要講下利用chrome瀏覽器的用戶配置的實現(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)站來說十分強大,但它也不是萬能的,實際上,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(); //后臺運行 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")); } }
沒錯,就是這么真實,對于常規(guī)網(wǎng)站可能沒什么反爬,但真正想要抓你還是一抓一個準(zhǔn)的。
說了這么多,是不是 selenium 真的不行?別著急,實際還是解決方法的。關(guān)鍵點在于如何在瀏覽器檢測之前將這些特征進行隱藏,事實上,前人已經(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)站試圖上傳到您計算機的任何 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)定性。因為如果選擇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實現(xiàn)控制瀏覽器的啟動選項Options的文章就介紹到這了,更多相關(guān)Java Selenium控制Options內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 刷題利器 LeetCode 插件詳解
這篇文章主要介紹了IntelliJ IDEA 刷題利器 LeetCode 插件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Jmeter 中 CSV 如何參數(shù)化測試數(shù)據(jù)并實現(xiàn)自動斷言示例詳解
這篇文章主要介紹了Jmeter 中 CSV 如何參數(shù)化測試數(shù)據(jù)并實現(xiàn)自動斷言,本文通過示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Java中replace與replaceAll的區(qū)別與測試
replace和replaceAll是JAVA中常用的替換字符的方法,下面這篇文章主要給大家介紹了關(guān)于Java中replace與replaceAll的區(qū)別與測試,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Spring MVC之@RequestMapping注解詳解
本篇文章主要介紹了Spring MVC之@RequestMapping 詳解,RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上。有興趣的可以了解一下。2017-01-01