简体   繁体   中英

How can I extract the content from a div using python + selenium

Im using python + selenium to scrape some data from a website and I encountered to following problem.

I have some divs of the following type: <div class = "element"> Data1 </div> <div class = "element"> Data2 </div> <div class = "element"> Data3 </div>

I selected the element using elements = driver.get_elements_by_class_name("element") then I looped through the elements and for each element I want to save the text in a list variable called dataArray my question is how can I select the text from the element in order to append it to the list?

for element in elements:
    dataArray.append(element.?)

you need .text method which is availabe in Selenium-Python bindings :

Instead of this :

for element in elements:
    dataArray.append(element.?)

use this :

for element in elements:
    dataArray.append(element.text)

you can simulate the same with .get_attribute('innerHTML') but I am not sure if you have the right locator for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM