从selenium webdrivers中的下拉列表中选择Enabled和Disables选项

时间:2016-11-16 05:51:49

标签: java selenium-webdriver

我是网络驱动程序的新手 我想从下拉菜单中选择启用和禁用选项之间的选项。 例如......将2选项作为

  1. Here i cannot click on **check-out-and-edit** as **undo-checkout is visible** 2。If this condition i continue with my code
  2. 所以场景是...... 如果撤消检查可见,我无法点击签出和修改选项。 所以我想要这个条件的代码示例,以便... 如果我的撤消结帐可见,那么我点击撤消检查,然后点击结帐 - 编辑选项,但如果我的退房和编辑已全部可见,然后我应继续使用我的代码..

    我的示例代码..

    boolean c1 = driver.findElement(By.linkText("Undo Checkout")).isEnabled();
    if(c1== true){
    
    }
    else{
    
    }
    

    但布尔只给我真正的价值......

2 个答案:

答案 0 :(得分:0)

I am assuming that u have something like below html code written for option u have give:

        <option value="0" ></option>
        <option value="1" >check-out-and-edit</option>
        <option value="2" disabled>undo-checkout</option>
The first try to fetch the list of options you have, then for each option in the list try to find the one having disable as attribute associated with it. For your reference I have given some sample code below. Hope this help. `List<WebElement> opts = temp.findElements(By.xpath(".//option")); WebElement dis_elem; for (WebElement opt : opts){ if(dis_elem = opt.getAttribute("disabled")!= null) { if(dis_elem.getText().equalsIgnoreCase("check-out-and-edit") { //Put the logic u want to execute in case undo-checkout option is eanble. } Else if if(dis_elem.getText().equalsIgnoreCase("undo-checkout") { // Put the logic u want to execute in case check-out-and-edit option is eanble. } } }` Else please post the HTML code of your page.

答案 1 :(得分:0)

首先使用以下代码获取undo-checkout的id ..

String e =driver.findElement(By.linkText("Undo Checkout")).getAttribute("id");

一旦你得到它然后把条件放在try和catch块中 如...

       try {
            WebElement e = driver.findElement(By.id("//id of undo-checkout"));
            undocheckout();
            }catch (Exception e) {
            WebElement e1 = driver.findElement(By.id("//id of checkout-and-edit""));
            checkoutandedit();
            }

如果第一个存在,则尝试执行,否则catch将执行...