Phantomjs through selenium in python

I am trying to check the behavior of a webpage for requests from different sources. I am doing the following so far

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.referer'] = referer 

The problem is that there are ajax requests on the web page that will change some things in html, and those ajax requests should have the web page as the referent, and not the referent that I gave at the beginning. It seems that the referent is set once at the beginning and each subsequent request is ajax, or the image or anchor accepts the same referent, and it never changes no matter how deep you look, is there a solution for choosing the abstract for the first request only and does it have a dynamic for the rest?

After some searching, I found this , and I tried to get it through selenium, but I haven't had time with this yet:

 webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.onInitialized'] = """function() {page.customHeaders = {};};""" 

Any ideas?

+6
source share
1 answer

From what I can say, you will need to fix PhantomJS to achieve this.

PhantomJS contains the GhostDriver module that provides the HTTP API that WebDriver uses to communicate with the PhantomJS instance. So, everything you want to do through WebDriver should be supported by GhostDriver, but it does not look like onInitialized supported by GhostDriver.

If you feel like an adventurer, you can clone the PhantomJS repository and fix src / ghostdriver / session.js to do what you want.

The _init method is as follows:

 _init = function() { var page; // Ensure a Current Window is available, if it found to be `null` if (_currentWindowHandle === null) { // Create the first Window/Page page = require("webpage").create(); // Decorate it with listeners and helpers page = _decorateNewWindow(page); // set session-specific CookieJar page.cookieJar = _cookieJar; // Make the new Window, the Current Window _currentWindowHandle = page.windowHandle; // Store by WindowHandle _windows[_currentWindowHandle] = page; } }, 

You can try using the code you found:

 page.onInitialized = function() { page.customHeaders = {}; }; 

in the page object created there.

Depending on what you are testing, although you can save a lot of effort and quit the browser and just test HTTP requests directly using something like the requests module.

+2
source

Source: https://habr.com/ru/post/971610/


All Articles