18

I lost my credentials.. so I'm creating this new thread. The old question it here if it helps: How to click a button to vote with python

I'd like to change this line:

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

to this:

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

So that the vote is set changing vote-link up to vote-link up voted.

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:

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()

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 data-faucet element, so I can replace it with vote-link up voted?

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

Anyway, this is the structure of the webpage:

<html>
<head></head>
<body role="document">
<div id="static page" class="container-fluid">
<div id="page" class="row"></div>
<div id="faucets-list">
<tbody>
<tr class=""></tr>
<tr class=""></tr>
<tr class=""></tr>
<tr class=""></tr>
# an infinite number of nodes, until there's mine
<tr class="">
<td class="vote-col">
<div class="vote-box">
<div class="vote-links">
<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=""></a>

The site is this: https://faucetbox.com/en/list/BTC

0

1 Answer 1

42

From comments:-

Do you want to interact with element which has data-faucet attribute value 39274??

exatly! just what I want to do!

You should try using css_selector as below :-

element = driver.find_element_by_css_selector(".vote-link.up[data-faucet = '39274']")

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

You can replace class attribute value using execute_script() as below :-

driver.execute_script("arguments[0].setAttribute('class','vote-link up voted')", element)
Sign up to request clarification or add additional context in comments.

5 Comments

ok.. now it selects something in fact if I print(element) terminal shows: <selenium.webdriver.remote.webelement.WebElement (session="d54ae232-6d42-455f-a130-097be89adf1e", element="{96385594-1725-4843-bfed-d5a4e7b9af41}")>. so now that I've selected it, how can I replace "vote-link up" with "vote-link up voted"?
Now try using execute_script() to set attribute value of the element, try with updated answer and let me know
just another thing... is it possible to do it with another library that does not open the firefox browser but that executes all in background? I don't want to know how to, just if is it possible
Yes, it's possible using headless browser, see this link for more details stackoverflow.com/questions/38549954/…
To make that more generic: driver.execute_script("arguments[0].setAttribute(arguments[1], arguments[2])", element, 'class', 'vote-link up voted')

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.