3

I'm new in using selenium, but I'm already read a lot of documentation and some code from websites. I want to write weight, and height to get BMI (ideal weight). There are 3 forms like weight form, height form (in feet) and height form (in inch). After writing in that form, Click Button Calculate to get BMI.

I write my code like this

        WebElement searchForm = driver.findElement(By.tagName("form"));
        searchForm.findElement(By.tagName("input"));
        WebElement searchBox = searchForm.findElement(By.name("weight"));
        searchBox.sendKeys("200");
        WebElement searchBox1 = searchForm.findElement(By.name("height1"));
        searchBox1.sendKeys("4");
        WebElement searchBox2 = searchForm.findElement(By.name("height2"));
        searchBox2.sendKeys("22");

My code doesn't work, so I tried an old code like this

        driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/span/input")).sendKeys("200");
        driver.findElement(By.xpath("/html/body/table/tbody/tr[2]/td[2]/span[1]/input")).sendKeys("4");
        driver.findElement(By.xpath("/html/body/table/tbody/tr[2]/td[2]/span[2]/input")).sendKeys("22");

I got error "org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='weight']"}" I don't know why weight can't get after using xpath.

I read on websites, they write the code and get the input form using driver.findElement(By.id("")); But in this HTML code, there is no ID.

this is HTML code

<table style="background-color: white;" width="300" border="0" cellpadding="2" cellspacing="0" height="100">
    <form name="bmi"></form>
        <tbody>
            <tr>
                <td align="right">
                    <span class="style4">Weight:</span>
                </td>
                <td>
                    <span class="style3">
                    <input type="text" class="bmi_txtbx" size="4" name="weight">
                        <select class="bmi_txtbx" value="lbs" name="weightuom">
                            <option selected="" value="lbs">lbs</option>
                            <option value="kg">kg</option>
                        </select>
                    </span>
                </td>
                <td></td>
            </tr>
            <tr>
                <td align="right">
                    <span class="style4">Height:</span>
                </td>
                <td colspan="2">
                    <span class="style3">
                    <input type="text" class="bmi_txtbx" size="4" name="height1">
                        <select class="bmi_txtbx" name="height1uom">
                            <option selected="" value="ft">ft.</option>
                            <option value="m">m</option>
                        </select>
                    </span>
                    <span class="style3">
                    <input type="text" class="bmi_txtbx" size="4" name="height2">
                        <select class="bmi_txtbx" name="height2uom">
                            <option selected="" value="in">in.</option>
                            <option value="cm">cm</option>
                    </select>
                    </span>
                </td>
            </tr>
            <tr align="right">
                <td colspan="3">
                    <hr size="1" noshade="noshade" align="left" width="200">
                </td>
            </tr>
            <tr>
                <td align="right">
                    <span class="style4">Your BMI is:</span>
                </td>
                <td colspan="2">
                    <span class="style3">
                    <input type="text" class="bmi_txtbx" size="5" name="bmi">
                    </span>
                    <span class="style3">
                    <input type="button" value="Calculate" onclick="calculateBMI()" name="button">
                    <input type="reset" value="Clear" name="Reset">
                    </span>
                </td>
            </tr>   
        </tbody>

</table>

Please help me solve this problem, any idea will very helpful for me. Thank you

1 Answer 1

1

The elements are available inside an iframe so You need to switch to iframe first to access the element.

Induce WebDriverWait() and frameToBeAvailableAndSwitchToIt() Induce WebDriverWait() and elementToBeClickable() and use the following Xpath.

WebDriverWait wait=new WebDriverWait(driver, 10);   
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='/Content/id-ID/html/Herbalife/standalone/BMI.html']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='weight' and @class='bmi_txtbx']"))).sendKeys("200");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='height1' and @class='bmi_txtbx']"))).sendKeys("4");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='height2' and @class='bmi_txtbx']"))).sendKeys("22");
Sign up to request clarification or add additional context in comments.

7 Comments

still have the same problem, and org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //input[@name='weight' and @class='bmi_txtbx'] (tried for 10 second(s) with 500 MILLISECONDS interval)
@jhon : Check if there any iframe on your page source.
yes that form is iFrame, I see it in console, BMI.html (iFrame)
Can you post that iframe html tag.because you need to switch this iframe first as i mentioned on my answer.
just go to top of the page and search iframe and post that html.
|

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.