Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get only text in div Tag except child element by Python selenium?

I want to get a text in the div tag by selenium
I want to get a text "APPLE".
this is the web site HTML

<div class="detail">
            <div class="fr-view fr-view-article"> APPLE <span style="color:#999999;"> BANANA </span></div>            </div>

So I wrote this code

content = driver.find_element('xpath', '//*[@id="BoardDelForm"]/div/div[1]/div[4]/div').text

xpath for

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="fr-view fr-view-article">

then I can see the text APPLE BANANA
the child element ‘BANANA’ is include in the text.

So I wroth with using another xpath

content = driver.find_element('xpath', '//*[@id="BoardDelForm"]/div/div[1]/div[4]/div/text()').text

then this error occured

The result of the xpath expression "//*[@id="BoardDelForm"]/div/div[1]/div[4]/div/text()" is: [object Text]. It should be an element.

how to get a only the text APPLE in this HTML?

>Solution :

You can try this code to get required text:

div = driver.find_element('css selector', 'div.fr-view-article')
print(driver.execute_script('return arguments[0].firstChild;', div)['textContent'])
# ' APPLE '
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading