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
4,698 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,910 points
How to replace remote resources with local resource in a page with Selenium  | Edureka Community
qlejclhn http://www.g3l79fg8g11d13u30k6vfuj2m9n62e4gs.org/
[url=http://www.g3l79fg8g11d13u30k6vfuj2m9n62e4gs.org/]uqlejclhn[/url]
<a href="http://www.g3l79fg8g11d13u30k6vfuj2m9n62e4gs.org/">aqlejclhn</a>

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 22, 2019 in Selenium by Abha
• 28,140 points
1,211 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,480 points
87,429 views
0 votes
1 answer

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

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

answered Jul 5, 2019 in Selenium by Anvi
• 14,150 points
20,591 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,190 points
6,126 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,500 points
13,674 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,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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 6, 2020 in Selenium by Sri
• 3,190 points
16,033 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,910 points
2,755 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP