javascript - 我应该如何在Python中使用这个onclick(javascript)

标签 javascript python mechanize

我是第一次在这里提问,我是 Python 新手。

我安装了 mechanize 和 BeautifulSoup 来更改页面中的某些表单。

现在,我使用br.submit()发送请求,不起作用!

有什么办法可以调用 onclick函数(javascript)?

这是有关该按钮发送数据的代码:

<div class="go_btm w_a1">
<p class="gogo"><a href="#" onclick="javascript:checkdata(document.mainform);" onkeypress="checkdata(document.mainform);">search</a></p>
<p class="gogo"><a href="#" onclick="reset();" onkeypress="reset();">cancel</a></p>
<br class="CLEAR" />
</div>

更新:

感谢您对 Selenium 这个工具的支持。

但我还有另一个问题。我的代码如下:

for i in range(len(all_options)): arr.append(all_options[i])
count = 0 for option in arr: print("Value is: %s" % option.get_attribute("value")) if count > 1: option.click() string = u'search' link2 = browser.find_element_by_link_text(string.encode('utf8')) response = link2.click() browser.back() count = count + 1

当我回到同一页面后,它回答我:

Traceback (most recent call last): File "C:\Users\pc2\Desktop\TEST.py", line 44, in <module> print("Value is: %s" % option.get_attribute("value")) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 93, in get_attribute resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name}) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 385, in _execute return self._parent.execute(command, params) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response raise exception_class(message, screen, stacktrace) StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=40.0.2214.111) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)

我只能点击一次选择。

这是否告诉我我在数组中的选项消失了?

我应该如何保留变量(选项)让下一个循环点击?

最佳答案

mechanize 无法处理 javascript:

相反,您可以通过selenium自动化真正的浏览器 。示例:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('myurl')

link = driver.find_element_by_link_text('search')
link.click()

关于javascript - 我应该如何在Python中使用这个onclick(javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423784/

相关文章:

python - 关于 Blender Python 中的模态运算符

python - python的 Mechanize 可以使用本地主机站点吗?

ruby - Mechanize 和提交文本区域的问题

python - Mechanize ._mechanize.FormNotFoundError : no form matching name 'q'

javascript - 跨存储库共享 react native 组件

javascript - 如何附加所有相同的类jquery?

python - 简单 python 日志记录配置中的重复输出

javascript - 使用 PhoneGap 的 iPhone 上的 OpenLayers 单击事件

javascript - 命中检测 Canvas JS

python - awk:致命:无法打开文件 'file' 进行读取(权限被拒绝)