简体   繁体   English

如何在硒webdriver中的onclick中使用if / else条件?

[英]How to use if/else condition with onclick in selenium webdriver?

In selenium webdriver i want to use if/else condition with java. 在Selenium Webdriver中,我想在Java中使用if / else条件。 Each steps need to be clicked and need to execute once has been selected . 每个步骤都需要单击,并且一旦选择就需要执行。 With the condition of Number_Select.NumberRandom(driver, 2).click(); 条件为Number_Select.NumberRandom(driver,2).click(); = selection 2 of if else statement from numberRandom. = numberRandom中if else语句的选择2。 For example , so my question is once the testcase run it only click one number , suppose to click 2 random number. 例如,所以我的问题是测试用例一旦运行就只单击一个数字,假设单击2个随机数字。 Kindly advise 好心提醒

TestCases : 测试用例 :

@Test(description = "Login Page Test" , enabled = true)
    public void faaaa() throws Exception {
        try{
            driver = new FirefoxDriver();
            SignIn_Action.Execute(driver);
            Menu_Select.Menu_Selection(driver, 2).click();
            MenuLottery_Select.Menu_Selection(driver, 1).click();
            MenuSubTab_Select.MenuSubTab_1(driver, 3).click();
            Number_Select.NumberRandom(driver, 2).click();
            //SignIn_Action.Logout_Session(driver);
            //Log.info("Login Successful");
        }catch (Exception e){

            System.out.println("Test 1"); 
            //Log.error(e.getMessage());
            throw (e);
        }

    }

Number_Select : Number_Select:

public static WebElement NumberRandom(WebDriver driver, int selection)
            throws Exception {

        if (selection == 1) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl/dd/i[8]"));
        } else if (selection == 2) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
        } else if (selection == 3) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
        } else if (selection == 4) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]"));
        } else if (selection == 5) {
            Thread.sleep(1500);
            element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[5]/dd/i[9]"));//*[@id="lottery"]/div[3]/dl[1]/dd/i[1]
        } else {
            Log.error("Unable to select product list");
            System.out.println("Unable select product list");
        }

        return element;
    }

NumberRandom is returning only one WebElement. NumberRandom仅返回一个WebElement。 Even if you are locating several WebElements in the same if block each time you override the last value element had and the last one is being returned to faaaa() . 即使您每次覆盖最后一个value element都在同一个if块中定位多个WebElement,并且最后一个element都返回给faaaa() If you want to click all the elements you need to insert the click() to the method 如果要单击所有元素,则需要在方法中插入click()

else if (selection == 2) {
    Thread.sleep(1500);
     element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")).click();
     element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")).click();
}

As a side note, you should avoid using Thread.sleep . 附带说明,您应该避免使用Thread.sleep If you want to make sure the element is clickable use explicit wait and expected conditions 如果要确保元素可单击,请使用显式等待和预期条件

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"))).click();

This will wait up to 10 seconds for the element to be clickable before clicking on it. 单击该元素之前,最多需要等待10秒钟才能单击该元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM