gpt4 book ai didi

python - Selenium Python 在不同条件下将 Text-var 链接到子子中的 Value-var

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:34 24 4
gpt4 key购买 nike

我有以下代码:

<div class="_97KWR">
<div class="_12x3s">
<div class="_1PF0y">
<div class="_1xNd0 CTwrR _3ASIK">
<a class="_3bf_k _1WIu4" href="/en/gilgamesh/hola-dola/gof-turkey-championship-2019-555/live/gazorpazorp/log-arena-fams-5551565"><span>Franco Pescatore</span></a>
<div class="_2cZpo">
<svg width="32px" height="32px" xmlns="http://www.w3.org/2000/svg">
<use xlink:href="dist/icons.svg#hula-hula"></use>
</svg>
</div>
<div class="_1sT8o _2ALVH">
<div class="_2Li3l _1fAz9"><img src="/dist/disabled-gg.png" alt="Disabled gg"></div>
</div>
</div>
<div class="_2b9x1">
<div>
<svg width="18px" height="14px" xmlns="http://www.w3.org/2000/svg">
<use xlink:href="dist/icons.svg#vs"></use>
</svg>
</div>
</div>
<div class="_1xNd0 CTwrR _3ASIK">
<a class="_3bf_k _1WIu4" href="/en/gilgamesh/hola-dola/gof-turkey-championship-2019-555/live/gazorpazorp/log-arena-fams-5551565"><span>Giorgio Pescato</span></a><img class="_25rWl _2cZpo" alt="Giorgio Pescato" src="https://ultramedia.com/Media/GiorgioPescato_f6f84978-6da4-4e14-a36b-860dce530d08.png">
<div class="_1sT8o _2ALVH">
<div class="_2Li3l _1fAz9"><img src="/dist/disabled-gg.png" alt="Disabled gg"></div>
</div>
</div>
</div>
<div class="_1xNd0 CTwrR">
<a class="_3bf_k _1WIu4" href="'/en/gilgamesh/hola-dola/swing/fun#4844844">
<span>BipolarFun</span></a><img class="_25rWl _2cZpo" alt="BipolarFun" src="https://promedia.com/Media//en/gilgamesh/hola-dola/swing/4844844.png">
<div class="_1sT8o _2ALVH">
<button class="_2Li3l _1fAz9"><span>1,30</span></button>
<div class="_1CeGR">
<div class="_1T5lR"></div>
</div>
</div>
</div>

它以这种方式动态地进行,由于 DIV 的类是动态的,因此我使用 xpath 进行“固定”

//a[starts-with(@href,'/en/gilgamesh/hola-dola/

有了这个,我可以访问像 Franco Pescatore 或 Giorgio Pescato 这样的名字并使用 xpath

//button[starts-with(@class,'_')]

我可以访问该值,但当它处于“禁用”状态时则无法访问

我也不能先做第一个,然后再做其他,因为它不会返回相同数量的元素,所以我在将 Text 元素与子子元素中的 Value 元素链接起来时遇到问题,

最后的结果应该是这样的:

n[0] = 'BipolarFun'
a[0] = 1,30
n[1] = 'Franco Pescatore'
a[1] = 0 #disabled
n[2] = 'Giorgio Pescato'
a[2] = 0 #disabled

请帮忙,我被困住了:|

最佳答案

您可以尝试的代码:

# get all labels
labels = driver.find_elements_by_xpath("//a[contains(@href,'/en/gilgamesh/hola-dola/')]")
for label in labels:
# get buttons inside label's parent DIV
buttons = label.find_elements_by_xpath("./ancestor::div[1]//button")
# if buttons more than 0, then get button text else use 0.
button_value = buttons[0].text if len(buttons) > 0 else 0
print(f"Label: {label.text}, value: {button_value}")

关于python - Selenium Python 在不同条件下将 Text-var 链接到子子中的 Value-var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57937468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com