2015-02-10 61 views
0

我是第一次在這裏提問,我是Python的新手。我應該如何在Python中使用這個onclick(javascript)

我安裝機械化和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> 

UPDATE:

感謝您的支持硒這個工具。

但我有另一個問題。我下面的代碼:

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)

我只能點擊一次選擇。

這是否說我在陣列中的選擇消失?

我應該如何讓變量(選項)讓下一個循環點擊?

+0

以這種方式運行javascript的唯一明智的方法是使用Selenium等瀏覽器(可能是無頭的)。 – 2015-02-10 03:57:14

+0

我很抱歉。我第一次來。 我想了解工具 – 2015-02-10 04:02:11

+0

你沒有做錯任何事,沒有理由道歉!我只是簡單地在@alexce的回答中給出了相同的信息。 – 2015-02-10 04:17:03

回答

1

mechanize無法處理JavaScript:

相反,你可以通過自動化selenium一個真正的瀏覽器。例如:

from selenium import webdriver 

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

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