简体   繁体   English

下拉单击在Selenium Java WebDriver中不起作用?

[英]Drop down click is not working in selenium java webdriver?

Could you please anyone tell me what is wrong in my code. 能否请任何人告诉我我的代码有什么问题。

WebElement dropdown = driver.findElement(By.xpath("//*[@id='idCallType_100']"));
        List<WebElement> Options = dropdown.findElements(By.tagName("option"));       
        System.out.println(Options.size());       
        for(int i=0;i<Options.size();i++)
        {
            WebElement o = Options.get(i);
            System.out.println(Options.get(i).getText()+ " ---- " + Options.get(i).getAttribute("value"));
            driver.findElement(By.xpath("//*[@id='idInsertTaskButton']/a")).click();         **// I couldnt able to click on this line**
            Options.get(i).click();
            System.out.println("coming for email time");
            driver.findElement(By.xpath("//*[@id='idInsertTask_100']/div/div[1]/input")).click();   

        }

I am trying to click on drop down. 我试图单击下拉菜单。 For the very first click its working fine. 首先,请点击它的正常工作。 form the next time its not executing from this line 形成下一次它不在此行执行
Options.get(i).click(); Options.get(i).click(); I dint know what is going wrong here. 我不知道这里出了什么问题。

Thanks!

Just wrap your WebElement into Select Object as shown below 只需将WebElement包装到Select Object中,如下所示

Select dropdown = new Select(driver.findElement(By.id("identifier")));

Once this is done you can select the required value in 3 ways. 完成此操作后,您可以通过3种方式选择所需的值。 Consider an HTML file like this 考虑这样的HTML文件

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

Now to identify dropdown do 现在确定下拉菜单

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do 要选择它的选项说“程序员”,您可以执行

dropdown.selectByVisibleText("Programmer ");

or 要么

dropdown.selectByIndex(1);

or 要么

dropdown.selectByValue("prog");

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

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