简体   繁体   中英

Scraping only text using selenium

I'm trying to scrape the text (dynamic) using selenium which is present as mentioned below in website. I searched all over the internet but I got how to scrape a text when it is present in separate div/p/span or any other tag only. Please help if you have any idea.

<div class='css-87uc0g e1tk4kwz1'>
    Text
    <span>
        4.5 
        <span> ::before </span>
    </span>
<div>

I want to extract only text here but that span tag is not allowing it.

Note: The value of text is dynamic.

Find the div element using following xpath and use javascript executor and get the first child value.

element=driver.find_element_by_xpath("//div[.//span[contains(.,'Rating')]]")
print(driver.execute_script('return arguments[0].firstChild.textContent;', element).strip()) 

Or you can simply replace the value.

element=driver.find_element_by_xpath("//div[.//span[contains(.,'Rating')]]")
print(element.text.replace(driver.find_element_by_xpath("//span[contains(.,'Rating')]").text,'').strip())

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