Python 如何使用硒更改元素类属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39434821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:15:40  来源:igfitidea点击:

How to change element class attribute value using selenium

pythonpython-3.xseleniumselenium-webdrivercss-selectors

提问by Thomas Machinton

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

我丢失了我的凭据.. 所以我正在创建这个新线程。如果有帮助的话,这里有一个旧问题: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 upto vote-link up voted.

以便投票设置更改vote-link upvote-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-faucetelement, so I can replace it with vote-link up voted?

但它显然不打印任何内容,因为第一个属性值有另一个数字。如何使用data-faucet元素数的输入选择我的行,以便将其替换为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

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

回答by Saurabh Gaur

From comments:-

来自评论:-

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

exatly! just what I want to do!

您想与具有data-faucet属性值的元素进行交互39274吗??

正好!正是我想做的!

You should try using css_selectoras below :-

您应该尝试使用css_selector如下: -

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

好的..现在它实际上选择了一些东西,如果我打印(元素)终端显示:<selenium.webdriver.remote.webelement.WebElement (session="d54ae232-6d42-455f-a130-097be89adf1e", element="{96385594-1725-4843-bfed-d5a4e7b9af41}")>.所以现在我已经选择了它,我如何用“投票链接投票”替换“投票链接”?

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

您可以class使用execute_script()以下方法替换属性值:-

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