Scraping only text using selenium

Viewed 288

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.

1 Answers

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())
Related