【发布时间】:2017-01-18 23:26:35
【问题描述】:
我丢失了我的凭据.. 所以我正在创建这个新线程。如果有帮助,这里的老问题:How to click a button to vote with python
我想改变这一行:
<a data-original-title="I like this 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 like this 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。
但问题是,在那个站点中,有几个项目要投票,并且“数据水龙头”元素发生了变化。如果我使用这个脚本:
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()
但它不打印任何东西,因为第一个属性值有另一个数字。如何通过输入data-faucet 元素的数量来选择我的行,以便将其替换为vote-link up voted?
我只能做这个硒?不使用真正的浏览器还有其他方法吗?
反正网页的结构是这样的:
<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>
【问题讨论】:
标签: python python-3.x selenium selenium-webdriver css-selectors