Search code examples
seleniumselenium-webdriverautomation

How to store the value from the list to a variable in selenium webdriver


enter image description hereI am new to selenium webdriver. I want to extract the value from the Ul class and store it in a variable but i am not able to do so

this is what i tried WebElement testuser = driver.findElement(By.cssSelector(".box ul.form li:nth-child(4)"));

div class="box" ul class="form"

  • User Type
  • School Administrator
  •         <li><h4>First Name</h4></li>
            <li>Faleata</li>
    

    Its saying unable to locate the elements


    Solution

  • To handle dynamic element use WebDriverWait and try the below xpath.

    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='box']/ul[@class='form']//li[./h4[text()='First Name']]/following::li[1]")));
    System.out.println(element.getText());