Python Selenium Javascript链接单击失败执行

时间:2013-05-31 18:41:51

标签: python selenium

我正在使用Selenium for Python和PhantomJS Ghost驱动程序,以便点击href中包含Javascript的链接,例如来自this链接的链接。我可以把链接当作一个元素,但是当我尝试点击它时,事情就出错了。这是一些代码(注意 - url是一个列表,第一个元素是Jscript链接,第二个是链接文本)

def get_jscript_down(self,url):
    driver = webdriver.PhantomJS()
    print(self.exact_url)
    driver.get(self.exact_url)
    elements = driver.find_elements_by_tag_name("a")
    for ele in elements:
        if ele.text == url[1].encode("utf-8").replace("  "," "):
            break
    ele.click()
    print("Cannot yet download %s" % url)

    return False

除了ele.click()之外,一切都按照我需要的方式工作。当我执行它时,我看到一个丑陋的WebDriverException。

selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click failed: ReferenceError: Can\'t find variable: __doPostBack\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:34441","User-Agent":"Python-urllib/2.6"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"71c20b50-ca21-11e2-a03c-f58c49e5a1bc\", \"id\": \":wdc:1370025577147\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/71c20b50-ca21-11e2-a03c-f58c49e5a1bc/element/%3Awdc%3A1370025577147/click"}' ; Screenshot: available via screen

我不确定为什么这次点击失败。据我所知,我应该可以用这种方式点击。下面是堆栈跟踪的相关部分(从事件开始分解到结束之前)

line 283, in get_jscript_down
    ele.click()
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 54, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 228, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
    raise exception_class(message, screen, stacktrace)

任何想法都将不胜感激。我很难过。

2 个答案:

答案 0 :(得分:2)

点击" Privatperson"时出现同样的错误。在this page。该按钮具有ID' btnSelectB2C'。我最终做的是在元素上使用submit:

driver.find_element(:id, 'btnSelectB2C').submit

(这是Ruby代码,但我认为如果它适用于您的情况,它应该是微不足道的。)

答案 1 :(得分:0)

问题在于 ele.click() 不在循环中。

试试这个:

def get_jscript_down(self,url):
    try:
        driver = webdriver.PhantomJS()
    except:
        print("[ERROR] Your Browser Have a problem")
    print(self.exact_url)
    try:
        driver.get(self.exact_url)
    except:
        print("[ERROR] Check Your Internet Connection , Cannot Open \"{0}\"".format(self.exact_url))
    try:
        elements = driver.find_elements_by_tag_name("a")
    except:
        print("[ERROR] Cannot Find Elements")
    for elem in elements:
        if elem.text == url[1].encode("utf-8").replace("  "," "):
            break
        else:
            elem.click()
    print("Cannot yet download {0}".format(url))
    return False