Selenium + Java Find the correct xpath

1

How are you?

I'm using Selenium with Java and I need to click on an option. The element is a radiobuttom.

<div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                        <label class="control radiobutton" data-hj-masked="">
                            30 días (TNA 24.50%)  (14/05/2018)
                            <input type="radio" name="radio" id="radio" value="0" checked="" data-hj-masked="">
                            <div class="radiobutton-indicator" data-hj-masked=""></div>
                        </label>
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                        <label class="control radiobutton" data-hj-masked="">
                            60 días (TNA 25.50%)  (12/06/2018)
                            <input type="radio" name="radio" id="radio" value="1" data-hj-masked="">
                            <div class="radiobutton-indicator" data-hj-masked=""></div>
                        </label>
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                        <label class="control radiobutton" data-hj-masked="">
                            90 días (TNA 25.50%)  (12/07/2018)
                            <input type="radio" name="radio" id="radio" value="2" data-hj-masked="">
                            <div class="radiobutton-indicator" data-hj-masked=""></div>
                        </label>
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                        <label class="control radiobutton" data-hj-masked="">
                            180 días (TNA 26.50%)  (10/10/2018)
                            <input type="radio" name="radio" id="radio" value="3" data-hj-masked="">
                            <div class="radiobutton-indicator" data-hj-masked=""></div>
                        </label>
                    </div>
            </div>

The problem is that, when I inspect the element in the option to click, I highlight label class="radiobutton control" data-hj-masked , and this is exactly the same in the 4 options. What differentiates them is the attribute "value" which is 0,1,2 and 3 respectively.

Any help to arm the xpath by selecting one of the options?

As a reference, the absolute xpath that the inspector copies is: / html / body / div [2] / div [4] / div [1] / div [2] / div / form / div [3] / div [2] / label / div

Of course, thank you very much!

Edit:

package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Plazo_Fijo {

    WebDriver driver;
    String urlprueba;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        urlprueba = "https://asdasd/login";
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
    }

    @Test
    public void PlazoFijoTradicional() throws InterruptedException {
        // Login
        driver.get(urlprueba);
        //Ingreso credenciales
        driver.findElement(By.id("DocumentNumber")).sendKeys("11111111");
        driver.findElement(By.id("UserName")).sendKeys("usuario");
        driver.findElement(By.id("Password")).sendKeys("password");
        driver.findElement(By.id("submitButton")).click();
        //Ingreso al menú y funcionalidad
        driver.findElement(By.linkText("Inversiones")).click();
        WebElement NuevaInv = driver.findElement(By.cssSelector("#buttons-container > button"));
        driver.findElement(By.xpath("//div[@class='box']/h3[normalize-space()='Plazo Fijo']")).click();
        driver.findElement(By.xpath("//*[@id=\"btn-submit\"]")).click();
        driver.findElement(By.id("ImporteVisible")).sendKeys("2000");
        driver.findElement(By.xpath("//*[@id=\"btn-submit\"]")).click();
        //Aquí el problema -->
        List<WebElement> resultados = driver.findElements(By.name("radio"));
        resultados.get(0).click();

Greetings!

    
asked by Cucho 13.04.2018 в 21:01
source

3 answers

0

Try the following:

// Obtiene todos los elementos con el nombre radio
List<WebElement> resultados = driver.findElements(By.name("radio"));

// Obtiene el primer elemento y lo clickea
resultados.get(0).click();
    
answered by 14.04.2018 в 21:13
0

Welcome to Stackoverflow:

For that you can use the following command that I show you, for example:

driver.findElement(By.id("radio")).click();
driver.findElement(By.xpath("(//input[@id='radio'])[2]")).click();
driver.findElement(By.xpath("(//input[@id='radio'])[3]")).click();
driver.findElement(By.xpath("(//input[@id='radio'])[4]")).click();

The first will click on the first radio button, the second on the second, and so on. This way you can simulate the click on a specific radio button.

    
answered by 15.04.2018 в 02:09
0

Well, after googling and doing some research, I found the reason why I could not find the element. Firefox was throwing this error at me:   org.openqa.selenium.ElementClickInterceptedException: Element is not clickable at point (589.7833251953125,343.5) because another element obscures it

The <label> tag is overlapping the <input> tag

Finally with:

//input[@value='3']//parent::label

It worked perfect. Thank you very much everyone for the feedback.

Greetings !!

    
answered by 16.04.2018 в 22:02