4

I have a script (inside <script></script> tags) that is being executed every time I load a page. Is it possible to remove an WebElement before the page being loaded in the WebDriver to prevent that script from executing?


I am thinking of something in the lines of:

Somehow get the raw HTML code (perhaps get source or something), remove the part (with selenium or parser), "inject" the edited code back into Selenium (Firefox WebDriver or maybe PhantomJS) and finally execute it for all pages on that website.

Is it possible to do that or is this perhaps impossible by design?

6
  • Do you have access to the original HTML? Commented Sep 26, 2016 at 15:00
  • I don't know what you mean by original. But I can see whats on the website. I can save that html as a string by using an appropriate method like driver.find_element_by_tag_name(<thefirsthtmltaghere>).get_attribute('outerHTML') (if I recall correctly, I am not at my main computer right now. But since I am trying to not run that script and not load that page it is useless since I cant find an element and thus get raw html data without loading the page. Commented Sep 26, 2016 at 15:04
  • 1
    Selenium will make a request and probably everything you will do is after the page has already loaded (and the script run). What you could do (it wouldn't be pretty, but...), is to create a complement to the browser you're running the tests (e.g. FF or Chrome) to remove this script for this URL and then save a profile of it. This profile could be indicated when starting the tests, what would prevent the script to run. Commented Oct 6, 2016 at 14:13
  • @Tom Thank you for your reply, Tom. So I should basically run the code as is, remove element by calling the specific method and then somehow save the browser state? Could you tell me what information I should be looking for on the Internet to make that solution possible? Commented Oct 6, 2016 at 20:17
  • 1
    @Clone, you can try to write a simple extension for your browser to "remove" that <script>, like it says here for FF: developer.mozilla.org/en-US/Add-ons/WebExtensions/…. When it's done and tested, then create a separated FF profile with your developed extension to run your tests, like it says here: toolsqa.com/selenium-webdriver/custom-firefox-profile. Hope you can make it! Commented Oct 7, 2016 at 11:00

1 Answer 1

1

If you install selenium-requests, you can make a GET request for the page, process the html/etc that is loaded, and then place in the tab.

It might be tricky to insert the processed result since you will likely need to also set the current browser URL to match (simply inserting it will cause issues with cross-domain loading of scripts, relative paths, etc) - perhaps there is a way of overriding (or allowing overriding) the 'get' response that selenium receives with the pre-processed information

Selenium-Requests makes a request using the requests library that uses the running webdriver's cookies for that domain and emulates the default HTTP headers sent by that webdriver. The result is a low-level HTTP request and response created with the webdriver's state. This is needed because the Selenium interface is very high-level, and doing much more than opening pages and exploring the DOM is not really natively possible in Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.