How to change element class attribute value using selenium
<p>I lost my credentials.. so I'm creating this new thread. The old question it here if it helps: <a href="http://stackoverflow.com/questions/39424703/how-to-click-a-button-to-vote-with-python">How to click a button to vote with python</a></p>

<p>I'd like to change this line:</p>

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

<p>to this:</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;span class="glyphicon glyphicon-thumbs-up"&gt;&lt;/span&gt;&lt;/a&gt;
</code></pre>

<p>So that the vote is set changing <code>vote-link up</code> to <code>vote-link up voted</code>.</p>

<p>But the problem is that in that site, there are severals items to vote, and the element "data-faucet" changes. If I use this script:</p>

<pre><code>from selenium import webdriver

driver = webdriver.Firefox()
driver.get("linkurl")
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 it obsiusly doesn't print anything, cause the first attribute value has another number. How can I select my line with the input of the number of <code>data-faucet</code> element, so I can replace it with <code>vote-link up voted</code>?</p>

<p>I only can do this selenium? Is there another way without using a real browser?</p>

<p>Anyway, this is the structure of the webpage:</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>The site is this: <a href="https://faucetbox.com/en/list/BTC" rel="nofollow">https://faucetbox.com/en/list/BTC</a></p>

<p>From comments:-</p>

<blockquote>
  <p>Do you want to interact with element which has <code>data-faucet</code> attribute value <code>39274</code>??</p>
  
  <p>exatly! just what I want to do!</p>
</blockquote>

<p>You should try using <code>css_selector</code> as below :-</p>

<pre><code>element = driver.find_element_by_css_selector(".vote-link.up[data-faucet = '39274']")
</code></pre>

<blockquote>
  <p>ok.. now it selects something in fact if I print(element) terminal shows: <code>&lt;selenium.webdriver.remote.webelement.WebElement (session="d54ae232-6d42-455f-a130-097be89adf1e", element="{96385594-1725-4843-bfed-d5a4e7b9af41}")&gt;.</code> so now that I've selected it, how can I replace "vote-link up" with "vote-link up voted"?</p>
</blockquote>

<p>You can replace <code>class</code> attribute value using <code>execute_script()</code> as below :-</p>

<pre><code>driver.execute_script("arguments[0].setAttribute('class','vote-link up voted')", element)
</code></pre>

python python-3.x selenium selenium-webdriver css-selectors