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.

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

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.

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

Any help would be hugely appreciated.

任何帮助将非常感激。

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.

以下代码仅用于打开www.google.ie并搜索“selenium”一词。对我来说它打开一个空白的Firefox浏览器并停止。

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()

7 个解决方案

#1


16

How about something like this

这样的事情怎么样?

PROXY = "149.215.113.110:70"

webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}

# you have to use remote, otherwise you'll have to code it yourself in python to 
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX)

You can read more about it here.

你可以在这里读更多关于它的内容。

更多相关文章

  1. 是否可以在没有安装铁python的PC上运行编译的铁python脚本?
  2. 怎样写贪吃蛇小游戏?用100行python代码轻松解决!
  3. 建模分析之机器学习算法(附python&R代码)
  4. 读取python中的unicode文件,该文件以与python源代码相同的方式声
  5. 从另一个Python脚本调用Python脚本的最佳方式是什么?
  6. CDays -4 可用的首个Python脚本
  7. Python脚本——连接到SSH并运行命令
  8. 自动完成在VS代码和Python中的自动化对象
  9. 毕业设计-基于深度神经网络的语音关键词检出系统-使用python脚本

随机推荐

  1. Android工具包AndroidUtils
  2. android.intent.action.MAIN与android.in
  3. 前台android与后台Servlet交互---上传文
  4. Android自动弹出软键盘(输入键盘)
  5. ANDROID GreenDao 使用例子 Android Gree
  6. 谷歌Android系统版本无序发展反噬产业链
  7. linux android 下进入android shell
  8. Android rxjava实现倒计时功能
  9. android 转屏问题
  10. 图片切换