需要从下拉列表中进行选择。如果选择"是",我需要填写某些文本字段,如果选择"否",则需要填写其他文本字段。使用seleniumjava,我需要关于如何编写if-else语句的帮助。我已经输入了完整的代码供您参考。
在此处输入代码
<WebElement JointApplication=driver.findElement(By.id("-1-1-joint_life"));
Select Joint=new Select(JointApplication);
Joint.selectByVisibleText("No");
Thread.sleep(6000);
if(driver.findElement(By.xpath("//SELECT[contains(@style,'width: 102%;')]")).isDisplayed())
{
// if we select yes for Joint Application
WebElement Title=driver.findElement(By.id("dd-2-1-title1"));
Select Tit=new Select(Title);
Tit.selectByVisibleText("Mr");
driver.findElement(By.id("-2-1-fullname1")).sendKeys("Test ");
driver.findElement(By.id("-2-1-csurname1")).sendKeys("Automatioan");
driver.findElement(By.id("-2-1-nino")).sendKeys("SK119944B");
driver.findElement(By.id("-1-d_o_b1")).sendKeys("01");
driver.findElement(By.id("-1-d_o_b2")).sendKeys("01");
driver.findElement(By.id("-1-d_o_b3")).sendKeys("1990");
}
else
{
WebElement COB=driver.findElement(By.id("-2-1-cntry_bth"));
Select birth=new Select(COB);
birth.selectByVisibleText("United Kingdom");
driver.findElement(By.id("-2-1-place_bth")).sendKeys("London");
Thread.sleep(2000);
WebElement Nationality=driver.findElement(By.id("-2-1-nat_ality"));
Select Nation=new Select(Nationality);
Nation.selectByVisibleText("United Kingdom");
}>
对于IF语句本身,执行
if(JointApplication.getAttribute("value").contains("No")){
//do this
} else {
//do this
}
应该起作用。它将查找select语句中设置的值。
尽管如此,我并不完全理解在这种情况下if语句的必要性。如果您知道要将其设置为"是"或"否",为什么不相应地填写其他字段呢?