user5760215
user5760215

Reputation:

Cannot open PhantomJS webpages in desktop mode (always in mobile mode)

I have been trying to fix this issue through stack overflow posts, but cannot find any relevant topics to my issue.

I am creating an automated python script that would automatically login to my facebook account and would utilize some features that facebook offers.

When I use selenium, I usually have the program run on the Chrome browser and I use the code as following

driver = webdriver.Chrome()

And I program the rest of the stuff that I want to do from there since it's easy to visually see whats going on with the program. However, when I switch to the PhantomJS browser, the program runs Facebook in a mobile version of the website (Like an android/ios version of Facebook). Here is an example of what it looks like

Mobile Version

I was wondering if anyone would be able to help me in try understanding how to convert this into desktop mode, since the mobile version of Facebook is coded differently than the desktop version, and I don't want to redo the code for this difference. I need to have this running on PhantomJS, because it will be running on a low-powered raspberry pi device that can barely open google chrome.

I have also tried the following to see if it worked, and it didn't help.

headers = { 'Accept':'*/*',                                                     
'Accept-Encoding':'gzip, deflate, sdch',                                    
'Accept-Language':'en-US,en;q=0.8',                                         
'Cache-Control':'max-age=0',                                                
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'                                  

}

driver = webdriver.PhantomJS(desired_capabilities = headers)                
driver.set_window_size(1366, 768)  

Any help would be greatly appreciated!!

Upvotes: 4

Views: 1204

Answers (1)

Volodymyr Herasymenko
Volodymyr Herasymenko

Reputation: 11

I had the same problem with PhantomJS Selenium and Python and next code was resolve it.

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities

desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
desired_capabilities['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' \
                                                                  'AppleWebKit/537.36 (KHTML, like Gecko) ' \
                                                                  'Chrome/39.0.2171.95 Safari/537.36'
driver = webdriver.PhantomJS('./phantom/bin/phantomjs.exe', desired_capabilities=desired_capabilities)
driver.get('http://facebook.com')

Upvotes: 1

Related Questions