Skip to content
Advertisement

Removing specific word from WebElement text

Hello I am scraping a website using selenium which has a button named view profile whenever i scrape it shows me the text of button in my output because it’s under the same <div> that I am scraping data from like

<div class="results">
   <p>example</p>
   <a href="www.example.com" rel="nofollow" class="search-result__button button button_s">View Profile</a>

my code

for pp in driver.find_elements_by_class_name('results'):
    print(pp.text)
    print('***********************')

its ouput

example text
example text
view profile
******************
example text
...

Is there any way I can remove that view profile text or stop it from printing?

Thanks

Advertisement

Answer

You can get the text of the button alone and remove it

for pp in driver.find_elements_by_class_name('results'):
    button_text = pp.find_element_by_class_name('search-result__button').text
    text = pp.text.replace(button_text, '')
    print(text)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement