在 Python 中使用代理运行 Selenium Webdriver [英] Running Selenium Webdriver with a proxy in Python

查看:39
本文介绍了在 Python 中使用代理运行 Selenium Webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 中运行 Selenium Webdriver 脚本来执行一些基本任务.当我通过 Selenium IDE 接口运行它时,我可以让机器人完美地运行(即:当简单地让 GUI 重复我的动作时).但是,当我将代码导出为 Python 脚本并尝试从命令行执行它时,Firefox 浏览器将打开但永远无法访问起始 URL(错误返回到命令行并且程序停止).无论我尝试访问什么网站等,这都会发生在我身上.

I am trying to run a Selenium Webdriver script in Python to do some basic tasks. I can get the robot to function perfectly when running it through the Selenium IDE inteface (ie: when simply getting the GUI to repeat my actions). However when I export the code as a Python script and try to execute it from the command line, the Firefox browser will open but cannot ever access the starting URL (an error is returned to command line and the program stops). This is happening me regardless of what website etc I am trying to access.

为了演示目的,我在这里包含了一个非常基本的代码.我认为我没有正确包含代码的代理部分,因为返回的错误似乎是由代理生成的.

I have included a very basic code here for demonstration purposes. I don't think that I have included the proxy section of the code correctly as the error being returned seems to be generated by the proxy.

任何帮助将不胜感激.

下面的代码只是为了打开 www.google.ie 并搜索单词selenium".对我来说,它会打开一个空白的 Firefox 浏览器并停止.

The below code is simply meant to open www.google.ie and search for the word "selenium". For me it opens a blank firefox browser and stops.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from selenium.webdriver.common.proxy import *

class Testrobot2(unittest.TestCase):
    def setUp(self):

        myProxy = "http://149.215.113.110:70"

        proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy':''})

        self.driver = webdriver.Firefox(proxy=proxy)
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.ie/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_robot2(self):
        driver = self.driver
        driver.get(self.base_url + "/#gs_rn=17&gs_ri=psy-ab&suggest=p&cp=6&gs_id=ix&xhr=t&q=selenium&es_nrs=true&pf=p&output=search&sclient=psy-ab&oq=seleni&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.47883778,d.ZGU&fp=7c0d9024de9ac6ab&biw=592&bih=665")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("selenium")

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

推荐答案

这种方式对我有用(类似于@Amey 和@user4642224 代码,但更短一点):

Works for me this way (similar to @Amey and @user4642224 code, but shorter a bit):

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)

这篇关于在 Python 中使用代理运行 Selenium Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆