首页 > 解决方案 > 需要 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");



    }>

标签: javaseleniumselenium-webdriverautomationautomated-tests

解决方案


对于 IF 语句本身,做

if(JointApplication.getAttribute("value").contains("No")){
  //do this
} else {
  //do this
}

应该管用。它将查找在 select 语句中设置的值。

虽然,我并不完全理解在这种情况下需要 if 语句。如果您知道将其设置为“是”或“否”,为什么不相应地填写其他字段?


推荐阅读