shalini
shalini

Reputation: 1

How can I get Text/result of an element in Selenium WebDriver?

  1. Visit www.viscosity.com and then mousehover to resources. Click on the Viscosity calculator
  2. Enter a numeric value in the first text box (tap to enter)
  3. Select any value from the list.
  4. Now see the result into the second text box

How can I print the result? Using gettext() in code doesn't print anything.

Upvotes: 0

Views: 1861

Answers (2)

shalini
shalini

Reputation: 1

    @Test
    public void menu() throws InterruptedException {

        WebElement Resource = driver.findElement(By.xpath("//div/div/span/div/ul/li[4]/a[1]"));
        Actions action = new Actions(driver);
        action.moveToElement(Resource).build().perform();

        driver.findElement(By.xpath(".//*[@id='hs_menu_wrapper_wizard_module_1059844378723389909774124948746072210']/ul/li[4]/ul/li[7]/a")).click();
        Thread.sleep(1000);

        WebElement element = driver.findElement(By.xpath("//div[1]/div/div/div/div/div/div/div[@class='cell-wrapper layout-widget-wrapper']/span/h1"));
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", element);

        driver.switchTo().frame(0);

        WebElement Dynamic_Textbox = driver.findElement(By.xpath("//div/center/div/div/div[1]/div[1]/div[@class='input-class']"));
        Actions actions = new Actions(driver);
        actions.moveToElement(Dynamic_Textbox);
        actions.click();
        double Textvalue = 6;
        actions.sendKeys(String.valueOf(Textvalue));
        actions.build().perform();
        System.out.println("Enter successfully");

        Select List_Value = new Select (driver.findElement(By.id("ddlFrom")));
        List_Value.selectByVisibleText("AFNOR Cup #4");

        if (List_Value.getFirstSelectedOption().getText().equals("AFNOR Cup #4"))
        {
            double cp = (4.8745*Textvalue - 46.668);
            System.out.println("Expected Result value=" + cp);
            String Cp_value = driver.findElement(By.id("txtCp")).getAttribute("value");
            System.out.println("Actual Result Value=" + Cp_value);
        }
    }

}

Upvotes: 0

Rohhit
Rohhit

Reputation: 742

This is what you are looking for:

public class Demo {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.viscosity.com/viscosity-calculator");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.switchTo().frame(0);
        driver.findElement(By.xpath("//*[@id='txtFrom']")).sendKeys("50");
        driver.findElement(By.xpath("//*[@id='ddlFrom']/option[3]")).click();
        String cp = driver.findElement(By.xpath("//*[@id='txtCp']")).getAttribute("value");
        System.out.println(cp);
        driver.quit();
    }
}

The gray background on which all the components / elements are placed is the iframe. First you need to switch to the iframe and then all the elements will be visible to the driver.

Instead of getText(), use:

getAttribute("value");

Upvotes: 1

Related Questions