Using selenium java I want to automate a survey page. The problem arises with a question with several sub-questions

Viewed 115

Using selenium java I want to automate a survey page. Every question has the same HTML code. The problem arises with a question with several sub-questions. Each subquestion has the same HTML code.

What I tried to do? I tried to rely on "name" and "value". After completion, the "Continue" button is displayed Locators

@FindBy(xpath = "//input[@name='select-12'][0]")
    WebElement question10;

    @FindBy(xpath = "//input[@name='select-12'][1]")
    WebElement question10a;

    @FindBy(xpath = "//input[@name='select-12'][2]")
    WebElement question10b;

    @FindBy(xpath = "//input[@name='select-12'][3]")
    WebElement question10c;

    @FindBy(xpath = "//input[@name='select-12'][4]")
    WebElement question10d;

    @FindBy(xpath = "//input[@name='select-12'][5]")
    WebElement question10e;

    @FindBy(xpath = "//input[@name='select-12'][6]")
    WebElement question10f;


    //executor.executeScript("arguments[0].click();", question10);
//        JavascriptExecutor js = (JavascriptExecutor) driver;
//        js.executeScript("window.scrollBy(0,200)", "");
        //executor.executeScript("arguments[0].click();", question10a);
        executor.executeScript("arguments[0].click();", question10b);
        executor.executeScript("arguments[0].click();", question10c);
        executor.executeScript("arguments[0].click();", question10d);
        executor.executeScript("arguments[0].click();", question10e);
        executor.executeScript("arguments[0].click();", question10f);
        executor.executeScript("arguments[0].click();", question11);
        disclaimer.click();
        continua.click();

Page HTML Code: HTML

The questioned image: Question image

How I can complete the question?

2 Answers

Question heading is different such as :

a. Activitati cotidiene si activitati ce tin de locul tau de munca

or

b. Hobiuri si activitati sportive

based on heading we can differentiate xpath (The below xpath is for first question):

//strong[contains(text(), 'a.')]/../descendant::input[@value='0']/..

for second or 3rd question all you have to do is to change the question heading in place of a. to that particular question heading like b. and so on..

and if you wanna click on 0, then leave this as it is input[@value='0'] but in case if you want to do click on 1, 2, 3, or any other number, you would have to change the number here :

input[@value='1']

or

input[@value='2']

and so on..

If it doesn't matter which option you select, you could do a List<WebElement> with the css locator '.sub-question .answers:nth-child(6) input' and select all items in a loop (the exemple is for selecting answer option '4' for each sub-question, you can change it as you want)

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

public class QuestionTenPage {
    WebDriver driver;

    @FindBy(css = ".sub-question .answers:nth-child(6) input")
    private List<WebElement> lastAnswerForSubQuestions;

    public void checkAllSubQuestions() {
        lastAnswerForSubQuestions.forEach(radio -> ((JavascriptExecutor)driver).executeScript("arguments[0].click();", radio));
    }
}
Related