PhantomJS + Selenium, How to use user agent for links that open in a new window?

I'm trying to click a link that opens in a new tab (target = "_blank") using selenium and phantomjs, but the problem is when phantomjs opens the link, it doesn't set a user-defined agent for it using DesiredCapabilities. If the link opens in the current window, everything is fine, but once it opens in a new window, the user agent is standard!

How can I install the user agent globally? (BTW I am using python)

+3


source to share


2 answers


After some investigation, I found that the problem seems to be related to the ghost driver. So I checked ghostub ghostub ghost and found the problem has been around since 2013 and there is a workaround for that. I am writing a solution here since it was not the result of a github issue on google search ( https://github.com/detro/ghostdriver/issues/273 ):

Guaranteed solution for target = "_blank" and window.open links - set phantomjs.page.customHeaders.User-Agent to the desired capabilities. In my case (window.open) it is possible to open a new window with the url "about: blank" and then find it in the handle array for activation and search.



Therefore, it is not yet recommended to set userAgent in custom headers in selenium docs, but currently it is the only way to set userAgent globally so that it does not change in new windows.

0


source


Try this code. working for me.



GetEval("window.open('Url')"); var winHandle = _sel.UnderlyingWebDriver.WindowHandles; _sel.UnderlyingWebDriver.SwitchTo().Window(winHandle[1]); _sel.UnderlyingWebDriver.Manage().Window.Maximize();

-1


source







All Articles