Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Selenium with PhantomJS empty page source

I'm having trouble with Selenium and PhantomJS on Windows7 when I want to get the source of the page of an URL. browser.page_source returns only <html><head></head></html>. I've put a sleep before browser.page_source but it didn't help.

This is my code:

from selenium import webdriver
browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe')
url = 'myurl'
browser.get(url)
print browser.page_source

On Linux with the same version of PhantomJS it works perfectly. Also it works on Windows Server 2003.

like image 827
Paul R. Avatar asked May 10 '14 12:05

Paul R.


People also ask

Does Selenium support PhantomJS?

"Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead" #48.

Is PhantomJS faster than Selenium?

Key Differences Between Selenium and PhantomJS Being a headless browser, the interactions are much faster than the real browser. So the performance time is smoother in PhantomJS than in Selenium.

How do I use Selenium PhantomJS?

Selenium PhantomJS To use the PhantomJS webdriver, all you need to do is change it to PhantomJS(). Then will This will work with both Python 2.7 and Python 3. On Windows, the path should be changed to the location of your phantomjs installation.

What is PhantomJS in Selenium?

PhantomJS is a headless Webkit, which has a number of uses. In this example, we'll be using it, in conjunction with Selenium WebDriver, for conducting basic system tests directly from the command line. Since PhantomJS eliminates the need for a graphical browser, tests run much faster.


3 Answers

by default phantomjs use SSLv3, but many sites after bug in ssl migrate to tls. That's why you has blank page. use service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
like image 146
user1032745 Avatar answered Oct 18 '22 02:10

user1032745


Using service_args=['--ignore-ssl-errors=true'] did the trick !

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])
like image 44
Paul R. Avatar answered Oct 18 '22 03:10

Paul R.


driverPhantom = webdriver.PhantomJS(driverLocation, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])      # initaling web driver for PhantomJs

Worked for me.

like image 31
Vardhman Patil Avatar answered Oct 18 '22 03:10

Vardhman Patil