conquester
conquester

Reputation: 1132

PhantomJS is not returning the page source

I have PhantomJS 1.9.0 installed.

I tried the following code to download the page and show the HTML.

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=['--ignore-ssl-   errors=true'])
page = driver.get('http://example.com')
print(page.page_source)

I get the following error:

/usr/bin/python3.4 phantom.py
Traceback (most recent call last):
File "phantom.py", line 5, in <module>
print(page.page_source)
AttributeError: 'NoneType' object has no attribute 'page_source'

Clearly it means that driver.get isn't working but the reason?

Upvotes: 1

Views: 1520

Answers (2)

Shrey
Shrey

Reputation: 532

The problem is that you're calling the page_source attribute on the page variable, which is incorrect. All the get method does is navigate to the URL specified in the parameter, but it does not return anything to be stored in the page variable, which is why the error indicates it is a NoneType object. What you need to do is call the attribute on the driver variable.

from selenium import webdriver
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
print(driver.page_source)

Upvotes: 2

bmcculley
bmcculley

Reputation: 2088

I currently don't have a good explanation as to why, but assigning page like that doesn't work for some reason

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
driver.get('http://example.com')
html_content = driver.page_source
print(html_content)
driver.close()

I'm going to keep looking and go read the documentation to see if I can find out why this is

Upvotes: 1

Related Questions