Selenium java 控制当前已经打开的 chrome浏览器窗口

首先来到安装 chrome浏览器 的文件夹下,例:C:\Program Files (x86)\Google\Chrome\Application

在此界面打开 cmd窗口,

然后输入:chrome.exe --remote-debugging-port=9527 --user-data-dir=“F:\selenium\AutomationProfile” ,并回车。
这句代码的意思是启动 chrome浏览器 的调试模式

  • user-data-dirr=“F:\selenium\AutomationProfile” 是在单独的配置文件中启动 chrome浏览器,可以理解为 新的浏览器,记得创建对应文件夹哦;
  • 其中 9527 为端口号,可自行指定。

此时候,如果无误的话就可以看到桌面新打开了一个 chrome 浏览器了。

编写 java 程序获取控制 浏览器

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class SeleniumOptions {
    static {
        /*
        浏览器驱动运行地址
         */
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\Desktop\\chromedriver_win32\\chromedriver.exe");
        /*
        浏览器运行地址
         */
        System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
    }

    public static void main(String[] args) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("debuggerAddress", "localhost:9527");
//        chromeOptions.addArguments()
        WebDriver  driver = new ChromeDriver(chromeOptions);

        driver.get("https://live.douyin.com/category/3_10000_2_2726");
        
        System.out.println(driver.getTitle());
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平静的大海

你的鼓励是我的动力源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值