skip to Main Content

There is a webpage with drop-down button with text inside. I want to retrieve text from that drop-down button, which is : "Description text".

Here is html code part:


<div data-v-e0a13c66="" data-v-5e9bf2df="" id="DetailDescription" class="detail-dropdown">
    <header data-v-e0a13c66="" class="detail-dropdown__header">
        <h5 data-v-e0a13c66="" class="detail-dropdown__title detail-dropdown__title--open">Описание</h5>
        <svg data-v-e0a13c66="" width="8" height="14"
            xmlns="http://www.w3.org/2000/svg" class="detail-dropdown__arrow--open detail-dropdown__arrow">
            <path data-v-e0a13c66="" d="M5.38 6.978c-.03-.02-.065-.036-.09-.06A10051.03 10051.03 0 0 1 .544 2.17C.202 1.83.154 1.335.424.962A.916.916 0 0 1 1.765.807c.032.027.061.057.091.087l5.42 5.42c.41.41.41.96 0 1.37L1.831 13.13c-.401.4-1.018.38-1.373-.046a.918.918 0 0 1 0-1.164c.033-.04.07-.078.108-.115L5.29 7.08c.025-.025.06-.04.09-.06v-.043Z"></path>
        </svg>
    </header>
    <div data-v-e0a13c66="" class="detail-dropdown__body">
        <article data-v-37bed4a0="" data-v-e0a13c66="" itemprop="description" class="detail-desc">
            <p data-v-37bed4a0="" class="detail-desc__text detail-desc__text--main">
                <p>Description text.</p>                <!---->                <!----></article>
        </div>
    </div>

but when i run this code:

from selenium import webdriver
from selenium.webdriver.common.by import By

def web_driver():
    options = webdriver.ChromeOptions()
    options.add_argument("--verbose")
    options.add_argument('--no-sandbox')
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument("--window-size=1920, 1200")
    options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(options=options)
    return driver


description_tags = driver.find_elements(By.XPATH, "//*[@*[contains(., 'detail-dropdown_body')]]")
list(map(lambda x: x.text, description_tags))

but the output is empty. How could i fix it?

2

Answers


  1. To get your element by XPATH you should use

    description_tags = driver.find_elements(By.XPATH, "//*[contains(@class, 'detail-dropdown__body')]")

    . literal in xpath contains searches for text, but actually you need to search for class, so instead . you should use @class.

    Login or Signup to reply.
  2. To extract the text Description text. ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR and text attribute:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.detail-dropdown__body > article.detail-desc p"))).text)
      
    • Using XPATH and get_attribute("innerHTML"):

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='detail-dropdown__body']/article[@class='detail-desc']//p[text()]"))).get_attribute("innerHTML"))
      
    • Note : You have to add the following imports :

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

    You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium – Python


    References

    Link to useful documentation:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search