如何在 selenium 中通过 https 禁用 dns

How to disable dns over https in selenium

我正在使用 selenium 编写测试,java,chrome。

如何在 chrome 设置中关闭“dns over https”?

我需要它,因为我的内网 DNS 的数据与互联网的不同。

我尝试添加以下选项。

    WebDriverManager.chromedriver().setup(); 
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    options.addArguments("ignore-certificate-errors");
    options.addArguments("--disable-async-dns");
    options.addArguments("--dns-prefetch-disable");
    options.addArguments("--disable-web-security");
    ChromeDriver driver = new ChromeDriver(options);

没用。 我什至尝试通过点击设置来更改选项

    driver.get("chrome://settings/security");
    String disableDNSOverHttpsButton = "/html/body/settings-ui//div[2]/settings-main//settings-basic-page//div[1]/settings-section[4]/settings-privacy-page//settings-animated-pages/settings-subpage/settings-security-page//settings-secure-dns//settings-toggle-button//div/cr-toggle";
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(disableDNSOverHttpsButton)));
    driver.findElement(By.xpath(disableDNSOverHttpsButton)).click();

有回复“org.openqa.selenium.NoSuchElementException:”

local_state = {
    "dns_over_https.mode": "off",
    "dns_over_https.templates": "",
}

 

options.add_experimental_option("localState", local_state)

解决方案是使用 setExperimentalOption。

    WebDriverManager.chromedriver().setup(); 
    ChromeOptions options = new ChromeOptions();
    Map<String, Object> localState = new HashMap<String, Object>();
    localState.put("dns_over_https.mode", "off");
    localState.put("dns_over_https.templates", "");
    options.setExperimentalOption("localState", localState) ;
    ChromeDriver driver = new ChromeDriver(options);