Hack Forums
[Python] PhantomJS useragent - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] PhantomJS useragent (/showthread.php?tid=5929274)



PhantomJS useragent - FW1738 - Jan 13, 2019

I am using PhantomJS with selenium and I am trying to change the user-agent, but it will not change. I studied the selenium code, and trying to change the headers, but nothing works. Does anybody have a solution?


RE: PhantomJS useragent - FriendZone - Jan 14, 2019

(Jan 13, 2019 04:14 PM)FW1738 Wrote:  I am using PhantomJS with selenium and I am trying to change the user-agent, but it will not change. I studied the selenium code, and trying to change the headers, but nothing works. Does anybody have a solution?

I usually use firefox or chrome with the headless option instead of phantomjs (during the years python support has been dropped often)

Anyway can you try this code:

Code
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87")

driver = webdriver.PhantomJS(desired_capabilities=dcap)



RE: PhantomJS useragent - lockhead - Jan 14, 2019

(Jan 14, 2019 03:20 AM)Hexecutor Wrote:  I usually use firefox or chrome with the headless option instead of phantomjs (during the years python support has been dropped often)

Since when do Chrome and Firefox have headless options?


RE: PhantomJS useragent - FriendZone - Jan 14, 2019

(Jan 14, 2019 07:59 AM)lockhead Wrote:  Since when do Chrome and Firefox have headless options?

Since always ahah

No, I’ m not sure when, but since years

In few mins I will post firefox headless


RE: PhantomJS useragent - lockhead - Jan 14, 2019

(Jan 14, 2019 08:13 AM)Hexecutor Wrote:  Since always ahah

No, I’ m not sure when, but since years

In few mins I will post firefox headless

Damn.. it's been a while since I needed something like this but a few years ago I had to deal with this PhantomJS BS because apparently it was the only option...


RE: PhantomJS useragent - FriendZone - Jan 14, 2019

(Jan 14, 2019 08:16 AM)lockhead Wrote:  Damn.. it's been a while since I needed something like this but a few years ago I had to deal with this PhantomJS BS because apparently it was the only option...

Code
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

binary = FirefoxBinary(‘BINARY PATH’)
options = Options()
options.set_headless(True)

driver = webdriver.Firefox(options=options, exacutable_path=‘GECKODRIVER PATH’ , firefox_binary=binary)

driver.browser.get(‘https://www.google.com’)


N.B.

I wrote the code without a pc close to me to test it .

Keep in mind firefox is slower than chrome in headless mode (I read time ago a speed test article about it)

Switch BINARY PATH and GECKODRIVER PATH with the right paths tell me if you need help


RE: PhantomJS useragent - FW1738 - Jan 14, 2019

(Jan 14, 2019 03:20 AM)Hexecutor Wrote:  I usually use firefox or chrome with the headless option instead of phantomjs (during the years python support has been dropped often)

Anyway can you try this code:

Code
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87")

driver = webdriver.PhantomJS(desired_capabilities=dcap)


Doesn't work for me. When I print the navigator.userAgent, I still get the same. I got phantomjs version 2.1.1, think that is the newest. That shouldn't be the problem. Does the code work for you?


RE: PhantomJS useragent - FriendZone - Jan 14, 2019

(Jan 14, 2019 09:24 AM)FW1738 Wrote:  Doesn't work for me. When I print the navigator.userAgent, I still get the same. I got phantomjs version 2.1.1, think that is the newest. That shouldn't be the problem. Does the code work for you?

As stated I don’t use phantomjs but firefox, so I didn’t test it, as soon I will be at home I will find a working way if possible :)


RE: PhantomJS useragent - FW1738 - Jan 14, 2019

(Jan 14, 2019 09:27 AM)Hexecutor Wrote:  As stated I don’t use phantomjs but firefox, so I didn’t test it, as soon I will be at home I will find a working way if possible :)

Thank you, do you know if firefox gets detected easily by for example recaptcha?


RE: PhantomJS useragent - FriendZone - Jan 14, 2019

(Jan 14, 2019 09:30 AM)FW1738 Wrote:  Thank you, do you know if firefox gets detected easily by for example recaptcha?

I would play with profiles and proxies.

Do you need an example to set it in firefox?


RE: PhantomJS useragent - FW1738 - Jan 14, 2019

No I know selenium pretty good, but I thought maybe you knew ways to detect the firefox driver besides the easy ones such as the navigator.webdriver flag.


RE: PhantomJS useragent - doon - Jan 14, 2019

I must add that running chrome headless vs firefox is a much greater gain if your looking for as little resource usage as possible.

Headless firefox and the non-headless version aren't that different in resource consumption, where as chrome is 30% less than it's non-headless version.