gpt4 book ai didi

python - 在 Selenium、Python 中查找下一个兄弟特定元素?

转载 作者:行者123 更新时间:2023-12-01 07:54:48 27 4
gpt4 key购买 nike

我需要先前选择的元素的同级元素。

例如:

<div class="dt">Technology</div>
<div class="da">GSM / CDMA / HSPA / EVDO / LTE</div>
<div class="dt">Dimensions</div>
<div class="da">149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)</div>
<div class="dt">Weight</div>
<div class="da">157 g (5.54 oz)</div>
<div class="dt">Build</div>
<div class="da">Back glass (Gorilla Glass 5), aluminum frame </div>
<div class="dt">Type</div>
<div class="da">Dynamic AMOLED capacitive touchscreen, 16M colors</div>
<div class="dt">SIM</div>
<div class="da">Single SIM (Nano-SIM) or Hybrid Dual SIM)</div>

假设我需要“尺寸”和类型。

代码:

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

if dt.("innerText") == "Dimensions":
print(dt.("innerText") + "-" + dt.**FollowingSibling**())
if dt.("innerText") == "Type":
print(dt.("innerText") + "-" + dt.**FollowingSibling**())

预期输出:

Dimensions - 149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)
Type - Dynamic AMOLED capacitive touchscreen, 16M colors

最佳答案

您可以根据需要使用 text 属性或 innerText。请尝试以下代码。

for dt in dts :

if dt.text == "Dimensions":
print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)
if dt.text == "Type":
print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)

或者

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

if dt.get_attribute("innerText")== "Dimensions":
print(dt.get_attribute("innerText") + "-" + dt.find_element_by_xpath("./following-sibling::div").text)
if dt.get_attribute("innerText") == "Type":
print(dt.get_attribute("innerText") + "-" + dt.find_element_by_xpath("./following-sibling::div").text)

输出:

Dimensions-149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)
Type-Dynamic AMOLED capacitive touchscreen, 16M colors

关于python - 在 Selenium、Python 中查找下一个兄弟特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039005/

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