Selenium webdriver.Firefox(proxy = proxy)无效

时间:2015-11-30 21:51:35

标签: python selenium proxy

  Simple java code minesweeper game use this below code. It  may be helpful for you
 Minesweeper.java

    import java.util.Random;
    public class Minesweeper {
      int _row=5,_col=5,_countGameOver=0;
      int [][] _board=new int[_row][_col];
      boolean [][] _flagMatrix=new boolean[_row][_col];
     public void plotBoard(){
       int noOfBomb=_col;
      while(noOfBomb>0) {
       int i=getRandomNumber();
       int j=getRandomNumber();
       if (_board[i][j]!=-1) {
      _board[i][j]=-1;
      noOfBomb--;
        }
       }
      }
    public int getRandomNumber(){
      Random t = new Random();
      // random integers in [0, 100]
      int num;
      {
      num=t.nextInt(_col);
      }while(num>_col);
      return num;
        }
    boolean isValid(int i,int j){
     return((i>=0 && i<_row)&&(j>=0 && j<_col));
    }
    int getAdjBomb(int i,int j){
     int num=0;
     //System.out.println("i="+i+"j="+j);
     for (int k = i-1; k <=(i+1); k++) {
     for (int l= j-1; l<=(j+1); l++) {
          if (isValid(k,l)&&(_board[k][l]==-1)) {
       num++;
          }
          }
     }
     return num;
    }
    public void calculateCounts(){
     for (int i = 0; i <_row; i++) {
          for (int j = 0; j <_col; j++) {
           if(_board[i][j]!=-1)
             _board[i][j]=getAdjBomb(i,j);
          }  
     }
    }
    public void print(){
     for (int i = 0; i <_row; i++) {
      for (int j = 0; j <_col; j++) {
       System.out.print(" "+_board[i][j]);
      }
      System.out.println("");
     }
    }
    public void displayFlagMatrix(){
     for (int i = 0; i <_row; i++) {
         for (int j = 0; j <_col; j++) {
          if (_flagMatrix[i][j]==true) {
           System.out.print(" "+"*");
       }
          else{
           System.out.print(" "+"+");
          }
         }
      System.out.println(" ");
     }
    }
    public boolean play(int r,int c){
     if (!(isValid(r,c))) {
      System.out.println("Please Enter row and col in correct range");
      return false;
     }
     if (_flagMatrix[r][c]==true) {
      System.out.println("This is a open position please enter other col and row");
      return false;
     }
     if (_board[r][c]==-1)
      return true;
     if (_board[r][c]!=0) {
      _countGameOver++;
      _flagMatrix[r][c]=true;
      }
     else{
      _flagMatrix[r][c]=true;
      _countGameOver++;
      openSpace(r, c);
     }
     return false; 
    }
    void openSpace(int r,int c){
      for (int k = r-1; k <=(r+1); k++) {
          for (int l= c-1; l<=(c+1); l++) {
                  if (isValid(k,l)&&(_board[k][l]!=-1)&&(_flagMatrix[k][l]==false)) {
                     _flagMatrix[k][l]=true;
                      _countGameOver++;
                  if (_board[k][l]==0) {
                      openSpace(k,l);
                   }
                 }   
           }
      } 
    }
    public boolean checkGameOver(){
     return(_countGameOver==(_row*_row)-(_row));
    }
    }

    ++++++++++++++++++
    MainMinessweeper.java

    import java.util.Scanner;
    import com.jan21.*;
    public class MainMinesweeper extends Minesweeper {
     public static void main(String[] args) {
      MainMinesweeper obj=new MainMinesweeper();
          obj.plotBoard();
          obj.calculateCounts();
          System.out.println("afate count");
          obj.print();
          int r,c;
          System.out.println("+++++++++++++++");
          obj.displayFlagMatrix();
          while(true){
         System.out.println("===============");
          Scanner input=new Scanner(System.in);
          System.out.println("enter row");
          r=input.nextInt();
          System.out.println("enter col");
          c=input.nextInt();
          if (obj.play(r,c)) {
       System.out.println("You Lose");
       break;
         }
       System.out.println("ok");
       if (obj.checkGameOver()) {
         obj.displayFlagMatrix();
        System.out.println("You Win Game");
        break;
       }
        obj.displayFlagMatrix();
        System.out.println("+++++++++++");
        obj.print();

        }
     }

    }

当定向到该网站时,它仍会显示我的本地代理。我从selenium网站上面得到了上述内容,但它没有使用我。

编辑:我刚尝试将格式更改为from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.proxy import * myProxy = "user:pass@proxy:port" proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy': myProxy, 'ftpProxy': myProxy, 'sslProxy': myProxy, 'noProxy': '' # set this value as desired }) driver = webdriver.Firefox(proxy=proxy) driver.set_window_size(1024, 768) driver.get('http://whois.urih.com/') ,这次代理似乎已经开始使用,因为现在打开对话框输入用户名并传递。那么这也不好。当然,我不需要尝试迂回的方式来关注警报并在那里输入信息......

1 个答案:

答案 0 :(得分:1)

这是为firefox webdriver设置代理的工作方法之一

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("network.proxy.type", 1)
firefox_profile.set_preference("network.proxy.http",ip) #set your ip
firefox_profile.set_preference("network.proxy.http_port", port) #set your port
driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get(url) #target url