How to click a button to vote with python
<p>I'm practicing with web scraping in python. I'd like to press a button on a site that votes an item.
Here is the code</p>

<pre><code>&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body role="document"&gt;
&lt;div id="static page" class="container-fluid"&gt;
&lt;div id="page" class="row"&gt;&lt;/div&gt;
&lt;div id="faucets-list"&gt;
&lt;tbody&gt;
&lt;tr class=""&gt;&lt;/tr&gt;
&lt;tr class=""&gt;&lt;/tr&gt;
&lt;tr class=""&gt;&lt;/tr&gt;
&lt;tr class=""&gt;&lt;/tr&gt;
# an infinite number of nodes, until there's mine
&lt;tr class=""&gt;
&lt;td class="vote-col"&gt;
&lt;div class="vote-box"&gt;
&lt;div class="vote-links"&gt;
&lt;a class="vote-link up" data-original-title="I like this faucet" href="#" data-faucet"39274" data-vote"up" data-toggle"tooltip" data-placement="top" title=""&gt;&lt;/a&gt;
</code></pre>

<p>And this it the final part but when I manually click on the button:</p>

<pre><code>&lt;a data-original-title="I&amp;nbsp;like&amp;nbsp;this&amp;nbsp;faucet" href="#" class="vote-link up voted" data-faucet="39274" data-vote="up" data-toggle="tooltip" data-placement="top" title=""&gt;&lt;/a&gt;
</code></pre>

<p>Can i simulate it with a script in python? I'm still a newbie and I've started learning python recently. P.S: the site is in https. And I cant's use http cause it force to redirect in https.</p>

<p>--UDPATE--
I'm trying with selenium.. </p>

<pre><code>from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://faucetbox.com/en/list/BTC")
element = driver.find_element_by_css_selector(".vote-link.up")
element_attribute_value = element.get_attribute("data-faucet")
if element_attribute_value == "39274":
    print ("Value: {0}".format(element_attribute_value))
driver.quit()
</code></pre>

<p>But since there are multiple number for each vote, it always shows the first one... so it never prints that print... How can I do to select my line of html sourcecode end replace it to a line I want?</p>

<p>You might want to check out <a href="http://selenium-python.readthedocs.io/" rel="nofollow">Selenium</a>. It is a module for python that allows you to automate tasks like the one you have mentioned above. Try something like this:</p>

<pre><code>from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://faucetbox.com/en/list/BTC")
elements = driver.find_elements_by_css_selector(".vote-link.up")
for element in elements:
    element_attribute_value = element.get_attribute("data-faucet")
    if element_attribute_value == "39274":
        print("Value: {0}".format(element_attribute_value))
        element.click()
        break
driver.quit()
</code></pre>

<p>The difference between what you did and what I did is that I used the <code>find_elements_by_css_selector</code> method instead of <code>find_element_by_css_selector</code>. The former returns a list of all elements that match the css selector while the latter returns only the first matching element. Then I just iterated through that list with a simple <code>for</code> loop and used the same check that you did. I also clicked the element in the loop. Hope this helps!</p>

python python-3.x web-scraping