selenium:使用已打开的chrome浏览器
生活随笔
收集整理的這篇文章主要介紹了
selenium:使用已打开的chrome浏览器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
- 環(huán)境參考 selenium:我的第一個(gè)程序
- 使用已打開的chrome瀏覽器有啥好處
- 不用驗(yàn)證登陸狀態(tài),可以先登陸,再爬蟲
- 不用反復(fù)開瀏覽器
開啟命令行啟動(dòng) chrome.exe
編寫程序
創(chuàng)建項(xiàng)目
在 Eclipse 中創(chuàng)建名為 02-UseOpenedChrome 的Maven項(xiàng)目。
添加依賴。
可以參考 selenium:我的第一個(gè)程序。
貼一下POM:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.sayyy.sample.selenium</groupId><artifactId>02-UseOpenedChrome</artifactId><version>1.0</version><properties><java.version>1.8</java.version><charset>UTF-8</charset><!-- javac -source --><maven.compiler.source>${java.version}</maven.compiler.source><!-- javac -target --><maven.compiler.target>${java.version}</maven.compiler.target><!-- 編譯時(shí)的編碼 --><maven.compiler.encoding>${charset}</maven.compiler.encoding><project.build.sourceEncoding>${charset}</project.build.sourceEncoding><project.reporting.outputEncoding>${charset}</project.reporting.outputEncoding></properties><dependencies><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.9.1</version></dependency></dependencies> </project>編寫 StartWebDriver 類
package net.sayyy.sample.selenium; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions;public class StartWebDriver {public static void main(String[] args) {System.setProperty("webdriver.chrome.driver", "C:\\selenum\\chromedriver_win32_80\\chromedriver.exe"); ChromeOptions options = new ChromeOptions();options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222"); //(1)WebDriver driver = new ChromeDriver(options);try {driver.get("https://taobao.com");} catch (Exception e) {e.printStackTrace();} finally {driver.quit(); //(2)}}}(1) 設(shè)置debuggerAddress。
(2) 退出 driver 。因?yàn)閏hrome不是driver創(chuàng)建的,所以退出driver不會(huì)關(guān)閉chrome。
啟動(dòng) StartWebDriver 類
啟動(dòng)后效果與 selenium:我的第一個(gè)程序 一樣。
結(jié)尾
至此完成了程序。
代碼參考: https://gitee.com/sayyy/sample-selenium-java/tree/master/02-UseOpenedChrome
總結(jié)
以上是生活随笔為你收集整理的selenium:使用已打开的chrome浏览器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: selenium:我的第一个程序
- 下一篇: 前端公共库CDN加速