0

How to select text content from multiple DIV elements using selenium?

On the website I intend to collect information it contains div and span with the same class.

How can I collect this information separately?

I need the contents inside the panel-body div > span of each block

driver.find_element_by_xpath(".//div[@class='panel-body'][1]/span[1]").text
driver.find_element_by_xpath(".//div[@class='panel-body'][1]/span[2]").text
driver.find_element_by_xpath(".//div[@class='panel-body'][1]/span[3]").text

driver.find_element_by_xpath(".//div[@class='panel-body'][2]/span[1]").text
driver.find_element_by_xpath(".//div[@class='panel-body'][2]/span[2]").text

html

        <div class="panel-heading">
            <h3 class="panel-title">Identificação</h3>
        </div>


        <div class="panel-body">
            <span class="spanValorVerde">TEXT</span><br>
            <span style="font-size:small;color:gray">TEXT</span><br>
            <br>
            <span class="spanValorVerde">TEXT</span>
        </div>


    </div>

    <div class="panel panel-success">

    
        <div class="panel-heading">
            <h3 class="panel-title">Situação Atual</h3>
        </div>


        <div class="panel-body">
            <span class="spanValorVerde">TEXT</span> <br>
            <span class="spanValorVerde">TEXT</span>
        </div>


    </div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
marcelo.delta
  • 2,730
  • 5
  • 36
  • 71
  • I am expecting that "select text" meant "get text", if you search with findElements with ".//div[@class='panel-body'][i]" will give you the total element present, then add another loop for .//div[@class='panel-body'][i]/span[j] and then get text. Hope it helps! – Durga Prasad Behera Jul 14 '20 at 02:39

2 Answers2

0

I am expecting that "select text" meant "get text".

first for loop:

count = driver.find_elements_by_xpath(".//div[@class='panel-body'][i]")

second for loop with count iteration:

driver.find_element_by_xpath(".//div[@class='panel-body'][i]/span[j]").text

If you search with findElements with ".//div[@class='panel-body'][i]" will give you the total element present, then add another loop for .//div[@class='panel-body'][i]/span[j] and then get text. Hope it helps!

0

To extract the texts e.g. TEXT, from each <span> using Selenium and you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute("innerHTML"):

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.panel.panel-success div.panel-body span.spanValorVerde")))])
    
  • Using XPATH and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='panel panel-success']//div[@class='panel-body']//span[@class='spanValorVerde']")))])
    
  • 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
    

Outro

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352