2

当我尝试使用 selenium 启动 mod 标头扩展时,我收到错误消息:“对服务器的请求已被扩展阻止。尝试禁用扩展。ERR_BLOCKED_BY_CLIENT”

我使用硒 3.0.4

硒中的代码:

package mrLogin;
import java.io.File;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.JavascriptExecutor;

 public class Login {
    public static String driverPath = 
"C:/auto/Mass_Retail_Auto/bin/mrLogin/";
    public static WebDriver driver ;
    public static JavascriptExecutor js=(JavascriptExecutor) driver;
    public static void main(String[] args) {
        try{        
        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", 
driverPath+"chromedriver.exe");
        driver = new ChromeDriver();
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File []{(new 
File("C:/auto/Mass_Retail_Auto/bin/mrLogin/modheader 2.1.2.crx"))});
        driver.navigate().to("chrome-
extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");
        js.executeScript(
                "localStorage.setItem('profiles', JSON.stringify([{                
" +
                "  title: 'Selenium', hideComment: true, appendMode: '',           
" +
                "  headers: [                                                      
" +
                "    {enabled: true, name: 'token1', value: '01234', 
comment: ''}, " +
                "    {enabled: true, name: 'token2', value: '56789', 
comment: ''}  " +
                "  ],                                                              
  " +
                "  respHeaders: [],                                                
   " +
                    "  filters: []                                                     
    " +
                "}]));                                                             
", args );

        driver.navigate().to("http://google.com");
        }
        catch(Exception e ){ 
            e.printStackTrace();

        }
        finally {
        //driver.close();
        System.out.println("Browser closed!!!");
        }
    }

 }
4

1 回答 1

0

我没有看到您的代码有任何重大问题。你可以试试修改后的代码块:

package mrLogin;
import java.io.File;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.JavascriptExecutor;

 public class Login {
    public static String driverPath = "C:/auto/Mass_Retail_Auto/bin/mrLogin/";
    public static WebDriver driver ;
    public static void main(String[] args) {
    try{        
        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
        driver = new ChromeDriver();
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File []{(new File("C:/auto/Mass_Retail_Auto/bin/mrLogin/modheader 2.1.2.crx"))});
        driver.navigate().to("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");
        JavascriptExecutor js=(JavascriptExecutor) driver;
        js.executeScript("localStorage.setItem('profiles', JSON.stringify([{  
        " +
                "  title: 'Selenium', hideComment: true, appendMode: '',           
        " +
                "  headers: [                                                      
        " +
                "    {enabled: true, name: 'token1', value: '01234', 
        comment: ''}, " +
                "    {enabled: true, name: 'token2', value: '56789', 
        comment: ''}  " +
                "  ],                                                              
          " +
                "  respHeaders: [],                                                
           " +
                    "  filters: []                                                     
            " +
                "}]));                                                             
        ", args );

        driver.navigate().to("http://google.com");
        }
    catch(Exception e ){ 
                e.printStackTrace();
        }
    finally {
        driver.close();
        System.out.println("Browser closed!!!");
        }
    }

 }
于 2017-11-18T12:47:20.240 回答