2

我正在将 Selenium for Python 与 PhantomJS Ghost 驱动程序一起使用,以便单击 href 中包含 Javascript 的链接,例如来自链接的链接。我可以将链接作为一个元素很好地获取,但是当我尝试单击它时,出现问题。这是一些代码(注意 - 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

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

第 283 行,在 get_jscript_down
    ele.click()
  文件“/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py”,第 54 行,点击
    self._execute(Command.CLICK_ELEMENT)
  _execute 中的文件“/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py”,第 228 行
    return self._parent.execute(命令,参数)
  文件“/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py”,第 165 行,在执行
    self.error_handler.check_response(响应)
  文件“/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py”,第 158 行,在 check_response
    引发异常类(消息、屏幕、堆栈跟踪)

任何想法将不胜感激。我很困惑。

4

2 回答 2

2

单击此页面上的“私人人员”时,我遇到了同样的错误。该按钮的 ID 为“btnSelectB2C”。我最终做的是在元素上使用提交:

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

(这是 Ruby 代码,但我认为如果它适用于您的情况,它应该很容易适应。)

于 2013-07-27T00:35:02.947 回答
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
于 2021-07-26T12:07:19.527 回答