1
<p class="field switch">
            <label id="on" class="cb-enable"><span>On</span></label>
            <label id="off" class="cb-disable selected"><span>Off</span></label>
</p>

Any idea how I can click the label with id="on"? It's a switch that I want to switch from off to on, by clicking the "on" label.

Things I've tried:

br.form.get(label="On").click()
br.form.find_control(id="on").click()

Documentation: http://wwwsearch.sourceforge.net/mechanize/forms.html

Any ideas?

User
  • 23,729
  • 38
  • 124
  • 207
  • What do you expect as the result? There's no JS support in Mechanize AFAIK, see also http://stackoverflow.com/questions/802225/how-do-i-use-mechanize-to-process-javascript. – ivan_pozdeev Nov 09 '14 at 20:28
  • The result should move `selected` to the `on` label. That's what happens when you click. – User Nov 09 '14 at 20:30

2 Answers2

2

Mechanize has no javascript support so there is no click event to trigger. Also, the HTML you are showing doesn't contain a input element so mechanize can't do anything with it.

If Javascript is truly needed for this site than I'd recommend using Watir, Selenium or Scrapy with ScrapyJS

With Mechanize you can manually edit the response like this:

import mechanize

class ReplacingOpener(mechanize.SeekableResponseOpener):
    def process_response_object(self, response):
        response = mechanize.SeekableResponseOpener.process_response_object(
            self, response)

        # Get the HTML from the response
        html = response.read()

        # Replace the HTML here with anything you feel like
        html = html.replace('</body>', '')

        # Set it back to the response
        response.set_data(html)
        return response


opener = mechanize.OpenerFactory(ReplacingOpener).build_opener()
response = opener.open('http://stackoverflow.com/questions/26793091/')

forms = mechanize.ParseResponse(response)
print forms
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • 1
    Isn't there a way just to modify the browser-side source and submit it? I just need to get `selected` in the `on` label. – User Nov 09 '14 at 20:39
  • 1
    There's no clear documented way to modify the source, but you could do so using a custom opener, I'll write an example. Give me a few minutes. – Wolph Nov 09 '14 at 20:52
  • @macdonjo: look at the example I've added :) – Wolph Nov 09 '14 at 21:19
0

It's a bit tricky with mechanize (see @Wolph's answer).

Why do you say that selenium is overkill? It would look like this:

from selenium import webdriver
url = "http://www.dummy.com/page.html"
click_id = 'on'
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_id(click_id).click()
new_page = driver.page_source
Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44