3

I want to extract all the text in a specific webpage.

In JavaScript the code looks like this:

var webPage = require('webpage');
var page = webPage.create();

page.open('http://phantomjs.org', function (status) {
    console.log('Stripped down page text:\n' + page.plainText);
    phantom.exit();
});

How can I run page.plainText in Python?

Thanks.

1
  • What is the selenium code you have tried till now? Commented Nov 1, 2017 at 12:10

2 Answers 2

10

If you want to do that with Selenium, you have to select the "top" element and after the call to getText().

For example, in Python:

driver = webdriver.PhantomJS(executable_path='pathTo/phantomjs')
driver.get('https://en.wikipedia.org/wiki/Selenium_(software)')
el = driver.find_element_by_tag_name('body')
print(el.text)
driver.close()
Sign up to request clarification or add additional context in comments.

Comments

2

Try this code:

text = driver.find_element_by_tag_name("body").get_attribute("innerText")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.