The first question is - do the questions needs to be in a random order? Part of being a QA is to give feedback on product decisions.. if it doesn't make sense, why is it part of the product design?
Given that there's a good reason why they're random..
If all of them share a similar div/html element ID, then you can use a CSS selector to grab a list of the similar elements, e.g:
By questionLocator = By.cssSelector("div[id*='question-']");
List<WebElement> questions = getDriver.findElements(questionLocator);
You can then iterate through that list to answer your questions. If it's multiple pages, loop this per page, like so:
By questionLocator = By.cssSelector("div[id*='question-']");
List<WebElement> questions;
while (morePages) {
questions = getDriver.findElements(questionLocator);
for (WebElement question : questions) {
// do something with question
}
morePages = areThereMorePages();
if (morePages) clickNext();
}
You'll have to code some extra logic to work out whether you're on the last page or not.
This approach only works if each question is indistinguishable from one another. It'll work pretty bluntly, and you can randomly fill in each question, but won't allow for any logic - nor will you be able to do any recall of your answers.
If you want to do something more complex, you will need to capture some identifying information from the web element. I've built tests for dynamic questionnaires, and the approach I took was to parse a unique question ID from the HTML element, and then query the application API/Database with that question ID, returning a Question object with which I could work out how to answer that specific question. This allowed a high degree of complexity, including multiple answers, optional answers, different types of question, quiz completion percentages..