gpt4 book ai didi

python - 如何使用 Selenium Python 定位元素

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

我正在尝试在以下 HTML 中使用 selenium/python 查找元素。类名会重复出现,并且检查器中的 xpath 选择器无法完成工作。我正在尝试获取 class="p_oaimY p_1ntuX" 中的美元金额数字。最接近的唯一 div ID 是 AppFrameMain。正如您所看到的,已经有一个元素具有相同的 class="p_oaimY p_1ntuX",其内容 Overview Dashboard 位于我想要查找的元素之前。

    <main class="p_2WHWf" id="AppFrameMain" data-has-global-ribbon="false">
<div class="p_IoKcO">
<div class="p_1fbVL p_qRiFk">
<div class="p_13E1y">
<div class="p_1jSPN">
<div class="p_1d9lu">
<div>
<h1 class="p_oaimY p_1ntuX">
Overview dashboard
</h1>
</div>
</div>
</div>
</div>
<div class="p_1RtSn">
<div class="_3x1oo">
<div testid="wrapper-component">
<div class="_1I7vu">
<div class="_2C0MX">
<button type="button" class="p_1wLbD p_1eWnt" tabindex="0" aria-controls="Popover1" aria-owns="Popover1" aria-haspopup="true" aria-expanded="false"><span class="p_3pWum"><span class="p_muTnj"><span class="p_2-hnq"><svg viewbox="0 0 20 20" class="p_v3ASA" focusable="false" aria-hidden="true">
<path d="M4 8h12V6H4v2zm9 4h2v-2h-2v2zm-4 0h2v-2H9v2zm0 4h2v-2H9v2zm-4-4h2v-2H5v2zm0 4h2v-2H5v2zM17 4h-2V3a1 1 0 1 0-2 0v1H7V3a1 1 0 1 0-2 0v1H3a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1z" fill-rule="evenodd"></path></svg></span></span><span class="p_1GgwJ">Year to date</span></span></button>
</div>
</div>
</div>
<div class="_20tm4">
<span class="p_3cBUM">compared to Jan 1–Apr 24, 2018</span>
</div>
</div>
<div class="_2ggUz">
<div class="_1pTom">
<div class="_2sn1O">
<div class="p_UC7bx">
<div class="p_3QlSz">
<div class="p_2XeKT p_33R3T p_2ieEg">
<div class="p_1fyLs">
<div class="_1lSbd">
<div class="_2HtyT">
<div>
<button type="button" class="_2VqzV" tabindex="0" aria-controls="Popover2" aria-owns="Popover2" aria-haspopup="true" aria-expanded="false"><span class="_3SvCs">Total sales</span></button>
</div>
</div>
</div>
</div>
<div class="p_1fyLs">
<div class="p_2XeKT p_33R3T">
<div class="p_1fyLs">
<div class="uXwm1">
<div class="p_2XeKT">
<div class="p_1fyLs p_KYKAN">
<p class="p_oaimY p_1ntuX">
$10,384.75
</p>
</div>
<div class="p_1fyLs">
<div class="Txt6X">
<p class="p_oaimY p_3ZtUV">
-
</p>
</div>
</div>
</div>
</div>
</div>

下面的 xpath 代码不起作用:

//*[@id="AppFrameMain"]/div/div/div[2]/div[2]/div[1]/div[1]/div/div/div/div[2]/div/div[1]/div/div/div[1]/p

最佳答案

要提取文本 $10,384.75,因为该元素是动态元素,您需要为 _visibility_of_element_ located_ 引入 WebDriverWait,并且可以使用以下命令解决办法:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Total sales']//following::p[1]"))).get_attribute("innerHTML"))

关于python - 如何使用 Selenium Python 定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832811/

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