java - 需要 Java 条件代码 Selenium 的帮助
问题描述
需要从下拉列表中选择。如果选择是,我需要填写某些文本字段,如果没有,则需要填写其他文本字段。使用 selenium java,我需要有关如何编写 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 语句。如果您知道将其设置为“是”或“否”,为什么不相应地填写其他字段?
推荐阅读
- python - 如何在 Python 中使用多年数据重塑数据框
- android - Android CTS 是否测试设备中已安装的应用程序?
- djaty - 为什么受 ID 计数影响的用户总是显示为零?
- react-native - 如何动态更改标题标题
- python - 从 sift.corpora 导入维基百科
- sql - 如何在 Oracle SQL 中将两个查询合二为一
- google-app-maker - Google App Maker:如何在绑定到计算模型的表上实现分页
- python - Tkinter:在其他图像之上显示图像
- postgresql - 有没有办法在 Debezium Postgres 连接器中启用支持以捕获复合类型列?
- elixir - Elixir:当前没有与给定名称关联的进程