使用Selenium自动化组合框(下拉+复选框)

时间:2014-12-02 06:53:11

标签: java selenium drop-down-menu

我正在尝试自动化网站Naukri.com中的下拉菜单。该下拉列表包含多选复选框。我们如何使用Selenium Web驱动程序自动化它?

下拉列表的结构是:

<div class="DDwrap">
<ul class="DDsearch">
    <li class="tagit" data-id="tg_indCja_a8_A">
         <span class="tagTxt">Accounting , Finance</span>
         <span class="dCross"></span>
    </li>
   <li class="frst" style="float: left;">
   <input id="cjaInd" class="srchTxt" type="text" placeholder="" name="" autocomplete="off"   style="width: 30px;">    
   <input id="hid_indCja" type="hidden" name="indType" value="["8"]">
   </li>
</ul>
</div>

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

查看下面的代码,导航到相关表单,打开&#34; Industry&#34;并选择两个复选框:&#39;会计,财务&#39;和政府,国防&#39;

WebDriver driver = new FirefoxDriver(); //Opening firefox instance

driver.manage().window().maximize(); //maximizing window
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds

driver.get("http://www.naukri.com/");


//Since there are two windows popping up, hence switching and closing the unnecessary window.
Set<String> windows = driver.getWindowHandles();        
Iterator iter = windows.iterator();
String parentWindow = iter.next().toString();
String childWindow = iter.next().toString();        
driver.switchTo().window(childWindow);
driver.close();
driver.switchTo().window(parentWindow);

//Hovering over "Jobs"
Actions act = new Actions(driver);
WebElement jobs = driver.findElement(By.xpath("//ul[@class='midSec menu']//div[.='Jobs']"));
act.moveToElement(jobs).build().perform();

//Clicking on "Advance Search"
WebElement Adv_search = driver.findElement(By.xpath("//ul[@class='midSec menu']/li[1]//a[.='Advanced Search']"));
act.moveToElement(Adv_search).click().perform();

//Clicking on the industry dropdown
driver.findElement(By.xpath("//div[@class='DDinputWrap']/input[contains(@placeholder,'Select the industry')]")).click();

//Selecting the checkbox containing text as "Accounting"
driver.findElement(By.xpath("//ul[@class='ChkboxEnb']//a[contains(text(),'Accounting')]")).click();

//Selecting the checkbox containing text as 'Government' 
driver.findElement(By.xpath("//ul[@class='ChkboxEnb']//a[contains(text(),'Government')]")).click();