0

Currently, my code looks like this:

for(int k=1; k<4; k++){
    String  elem1 = driver.findElement(By.xpath("/html/body/div[4]/div/div/div[2]/div/div/div/div[3]/div[2]/div[2]/div[2]/ul[1]/li[6]/div/ul/li/div/a[k]" + k)).getAttribute("innerHTML");
    //String  elem2 = driver.findElement(By.xpath("/html/body/div[4]/div/div/div[2]/div/div/div/div[3]/div[2]/div[2]/div[2]/ul[1]/li[6]/div/ul/li/div/a[2]")).getAttribute("innerHTML");
    //String  elem3 = driver.findElement(By.xpath("/html/body/div[4]/div/div/div[2]/div/div/div/div[3]/div[2]/div[2]/div[2]/ul[1]/li[6]/div/ul/li/div/a[3]")).getAttribute("innerHTML");

    System.out.println(elem1);
    //System.out.println(elem2);
    //System.out.println(elem3);

    Thread.sleep(3000);
    double value = Double.parseDouble(elem1);
    System.out.println(value);
}

As you can see in the String elem1 line, I try to pass the value k into XPath, but it doesn't work. How can I do that? I need to increase the value of k because as you can see in the commented lines it goes: 1-2-3-4...-78...

0

1 Answer 1

1

You need to put the variable k in the exact place you want its value:

By.xpath("/html/body/div[4]/div/div/div[2]/div/div/div/div[3]/div[2]/div[2]/div‌​[2]/ul[1]/li[6]/div/ul/li/div/a[" + k + "]")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.