Bronson77
Bronson77

Reputation: 289

How to locate the element using Selenium Python

I'm trying to locate an element using selenium / python in the following HTML. The class names repeat themselves and the xpath selector in inspector isn't getting the job done. I'm trying to get dollar amount figure in class="p_oaimY p_1ntuX". The closest unique div ID is AppFrameMain. As you can see there's already one element with the same class="p_oaimY p_1ntuX" with the contents Overview Dashboard preceding the one I'm wanting to find.

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

The xpath code below isn't working:

//*[@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

Upvotes: 1

Views: 265

Answers (2)

KunduK
KunduK

Reputation: 33384

Use WebDriverWait to handle dynamic element and 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)

Output:

$10,384.75

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193058

To extract the text $10,384.75 as the element is a dynamic element you need to induce WebDriverWait for the _visibility_of_element_located_ and you can use the following solution:

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

Upvotes: 1

Related Questions