如何使用硒更改元素类属性值 [英] How to change element class attribute value using selenium

查看:48
本文介绍了如何使用硒更改元素类属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我丢失了凭据..所以我正在创建这个新线程.这里有一个老问题,如果有帮助的话:如何单击按钮用python投票

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

我想更改此行:

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

对此:

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

以便设置投票,将vote-link up更改为vote-link up voted.

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

但是问题是,在该站点中,有几个项目需要投票,并且元素"data-faucet"发生了变化.如果我使用此脚本:

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>

网站是这样的: https://faucetbox.com/en/list/BTC

推荐答案

来自注释:-

是否要与具有data-faucet属性值39274的元素进行交互?

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

确实!就是我想做的事!

exatly! just what I want to do!

您应尝试按以下方式使用css_selector:-

You should try using css_selector as below :-

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

好..现在,如果我在print(element)终端上显示以下内容,它实际上会选择某项内容:<selenium.webdriver.remote.webelement.WebElement (session="d54ae232-6d42-455f-a130-097be89adf1e", element="{96385594-1725-4843-bfed-d5a4e7b9af41}")>.因此,既然我已经选择了它,那么如何替换"vote-link up"?带有投票链接投票"?

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"?

您可以使用execute_script()替换class属性值,如下所示:-

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

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

这篇关于如何使用硒更改元素类属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆