How to replace remote resources with local resource in a page with Selenium?

0 votes

Is it possible to run an automatic test on a web page with Selenium and force it to replace the remote resource with another one in local?

Like replacing this:

<html>

  ...

  <script href="http://cnd.foo.bar/lib.js"/>

  ...

</html>

with

<html>

   ...

   <script href="http://localhost:3000/lib.js"/>

   ...

</html>

Aug 8, 2018 in Selenium by Martin
• 4,320 points
84 views

1 answer to this question.

0 votes

You could change URL for that script with a command:

<tr>

    <td>runScript</td>

    <td>document.querySelector('script[src=&quot;http://cnd.foo.bar/lib.js&quot;]').setAttribute('src', 'http://localhost:3000/lib.js')</td>

    <td></td>

</tr>

But when you execute your Selenium script browser is allready downloaded CDN javascript and nothing will happen.

You could delete reference to CDN js add reference to local javascript to your DOM :

<tr>

    <td>runScript</td>

    <td>var sc = document.querySelector('script[src=&quot;http://cnd.foo.bar/lib.js&quot;]');sc.parentNode.removeChild(sc);</td>

    <td></td>

</tr>

<tr>

    <td>runScript</td>

    <td>var js = document.createElement('script'); js.src = 'http://localhost:3000/lib.js'; document.head.appendChild(js);</td>

    <td></td>

</tr>

That will load new javascript. But maybe some scripts from CDN will be executed on page load.

IMO best solution is run localhost on port 80 and then set CDN url to 127.0.0.1 in hosts file.

answered Aug 8, 2018 by Samarpit
• 5,850 points

Related Questions In Selenium

0 votes
1 answer

How to create a test case with TestNG in Selenium?

Hey Deepak, to create a Test Case ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 27,740 points
97 views
0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
4,294 views
0 votes
1 answer

How to scroll up/down a page using Actions class in Selenium?

Hi Naetik, you can use Actions class ...READ MORE

answered Jul 4, 2019 in Selenium by Anvi
• 14,010 points
2,166 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
1,543 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,850 points
1,486 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,850 points
1,265 views
0 votes
1 answer

How to open a browser window in full screen using Selenium WebDriver with C#

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,850 points
5,272 views
0 votes
1 answer

How to login a forum using Selenium with Python

You should try to directly log in ...READ MORE

answered Apr 27, 2018 in Selenium by Samarpit
• 5,850 points
424 views