20

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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Paul R.
  • 322
  • 1
  • 2
  • 10
  • Try some debugging, capture a screenshot for example – Furious Duck May 12 '14 at 21:31
  • 3
    The screenshot is empty, blank image. I thought that it's a problem with PhantomJS and I've tried to load the page with PyQt4's webkit but the result is the same, just and tags. This is the URL which gives me this headache: https://www.homesearch.com/browse?fulltextquery=miami+fl&page=0 – Paul R. May 13 '14 at 22:33
  • I've encountered the same issue when navigated to some _https_://url. Use `time.sleep(few_seconds)` or webdriver's [expected conditions](http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.support.expected_conditions.presence_of_element_located) to wait for some element to be present on page after calling `browser.get(url)` – Furious Duck May 13 '14 at 23:47
  • also try to creat a driver instance with these params `browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])` – Furious Duck May 14 '14 at 11:38
  • I've used a sleep of 15-20 seconds and the result was the same. I will try with service_args. – Paul R. May 14 '14 at 17:15

4 Answers4

35

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'])
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
user1032745
  • 398
  • 3
  • 4
  • 1
    Upvoted this because using just the ignore ssl error did not work for me but adding ssl protocol any suggested by this comment help. Kudos – freonix Dec 19 '15 at 02:49
  • Be aware that `--ignore-ssl-errors` is dangerous and you shouldn't use it unless you know what you're doing. The other option will probably fix most issues. – Alex W Jul 26 '16 at 16:31
  • Thank you! Saved me allot of time. – Markvds Mar 14 '17 at 13:09
  • @AlexW this solution is worked for me, but I don't know under the hood about this. Could you elaborate about the dangerous using `--ignore-ssl-errors` or provide some reference about this? – Adiyat Mubarak Mar 29 '17 at 04:59
  • I have a related question: `How can we found these selenium phantomjs usages?` I think it's very hard for me to find these usages in http://www.seleniumhq.org/docs/ – Cloud May 13 '17 at 03:02
8

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'])
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
Paul R.
  • 322
  • 1
  • 2
  • 10
  • I am using `webdriver.Remote` as I am running a phantomJS remotely. Do you know how to pass the service_args if this is the case? – Nadun Perera May 17 '19 at 01:22
2
driverPhantom = webdriver.PhantomJS(driverLocation, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])      # initaling web driver for PhantomJs

Worked for me.

Vardhman Patil
  • 517
  • 6
  • 22
0

increasing the screen size as below worked for me:

driver = webdriver.PhantomJS(path2phantom, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 
driver.set_window_size(2000, 1500)