0

I'm first time to ask questions here and I'm new to Python.

I install the mechanize and BeautifulSoup to change some forms from a page.

Now, I use br.submit() to send the request , it doesn't work!

Is there any way to call the onclick function(javascript)?

Here is the code about that button send data:

<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:

Thank you for support the Selenium this tool.

But I have another problem. My code below:

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

After I back to the same page,it answer me:

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)

I can only click the select once.

Is that talk me my option in the array disappear?

How should I keep the variable(option) let next loop to click?

3
  • The only sensible way to run javascript this way is to involve a browser (possibly headless) e.g with Selenium. Commented Feb 10, 2015 at 3:57
  • I'm so sorry. I'm first time here. I'm trying to understand the tools Commented Feb 10, 2015 at 4:02
  • You did nothing wrong, no reason to apologize! I'm just giving the same info very concisely in a comment as @alexce did in their answer. Commented Feb 10, 2015 at 4:17

1 Answer 1

1

mechanize cannot handle javascript:

Instead, you can automate a real browser via selenium. Example:

from selenium import webdriver

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

link = driver.find_element_by_link_text('search')
link.click()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.