Selenium - Find out where script was downloaded fromAsk Question

问题:

I have to validate that a web application, when executed in the client browser, is fetching some assets (*.js) from a particular remote server.

Say two options exist: whether it gets the script from server A or it gets a copy from server B. I need to assert (based on some preconditions) that the script was downloaded from server A.

The question: Is there a way to inspect source url of loaded javascript using selenium (preferably with python)?

回答1:


Here it is a possible solution to extract url of javascript libraries from the stackoverflow site.

You should adapt the solution to the site you are working on.

driver = webdriver.Firefox()
driver.get("http://www.http://stackoverflow.com/")
link= driver.find_elements_by_tag_name('script')
for i in link:
    print i.get_attribute("src")

Example of output:

http://rules.quantcount.com/rules-p-c1rF4kxgLUzNc.js
http://edge.quantserve.com/quant.js
http://b.scorecardresearch.com/beacon.js
https://www.google-analytics.com/analytics.js
https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
https://cdn.sstatic.net/Js/stub.en.js?v=9798373e8e81

There are various strategies to locate elements in a page. You can use the most appropriate one for your case (http://selenium-python.readthedocs.io/locating-elements.html)

标签: selenium
© 2014 TuiCode, Inc.