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,340 points
762 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,890 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
• 28,040 points
350 views
0 votes
3 answers

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

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,440 points
31,810 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,150 points
12,414 views
0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
2,841 views
0 votes
2 answers

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,520 points
5,113 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,842 views
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,890 points
3,605 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,349 views
0 votes
2 answers

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

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,010 points
11,882 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,890 points
1,142 views