如何使用Selenium Python定位元素

时间:2019-04-24 14:43:35

标签: python selenium selenium-webdriver webdriver webdriverwait

我正在尝试在以下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

2 个答案:

答案 0 :(得分:0)

使用WebDriverWait处理动态元素和Css Selector

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


wait = WebDriverWait(driver, 20)
element=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#AppFrameMain p.p_oaimY.p_1ntuX')))
print(element.text)

输出:

$10,384.75

答案 1 :(得分:0)

要提取文本 $ 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"))