I have to use multiple URLs in same script in Selenium (Java) and have to visit one at a time, perform some operations on the page take a screenshot at the end, and then move one to the next page and repeat the same steps. Is this task doable with Selenium? I know it can be done through loops otherwise but can I do it with Selenium?

P.S. I'm a beginner in Selenium and I have searched the internet thoroughly before posting the question without finding a solution.

  • What do you mean under doing with loop VS doing with selenium? You can combine power of loops with features of selenium. Can you add some example you have.. Like how you iterate over 1 url.
    – Alexey R.
    Jun 3 at 14:07

If we sum up the OPs question as:

Can I use Selenium to test multiple URLs?

Then the answer is: Yes.

Like with most questions in Selenium, the answer, in part, is using Page Objects (POMs). I'd setup any repeatable code in a Page Object that you then use in your test files.

An example with the OPs question, I'd setup a navigation class that loads each URL; separate classes with page elements and methods that represent the different pages you navigate to; a utilities method that handles screenshots. So far, none of this is using loops.

With the test files, I'd have one test file for each URL. Use a beforeAll to open the URL that calls your navigation class; have one or many test methods that perform some operation; have an afterTest which takes a screenshot after each test. Again, repeat this for as many URLs you need to test. This then allows the test runner to run all the tests. Again, no loops are needed.

If you don't want or need separate test files for each URL, then you can handle URL reload on each test method.


Yes, this is achievable through Selenium. Given that selenium is an automation tool, and you can write your script logic in languages like Java or Python there is nothing stopping you from writing a script mentioned above, respective to software testing service.

A straight forward logic would be,

(pseudo code)

test(Webdriver driver){
List<String> urlList = getUrls();
for(String url : urlLists){


A more test-centric approach would be to pass url as test data, so test would run n times for n urls.

test(Webdriver driver, String url){    

You can further refactor using Page Object Model as per your specific use case with respect to your application.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.