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