【问题标题】:How to locate the element using Selenium Python如何使用 Selenium Python 定位元素
【发布时间】:2019-04-24 14:43:34
【问题描述】:

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

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    使用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
    

    【讨论】:

    • 感谢您的评论。我试图找到包含文本“$10,384.75”的“p”标签。 “p”标签在整个文档中重复出现(未显示)。
    • 好吧,这是你第一个孩子的价值吗?我已经根据你的 html 更新了
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2021-05-03
    相关资源
    最近更新 更多