通过selenium选择javascript下拉?

时间:2015-03-13 05:59:04

标签: java javascript selenium selenium-webdriver

下面是javascript下拉列表,我想通过selenium webdriver选择“All Resume”选项:

<div id="resume_freshness_container">
<div class="dropdown_small_wrapper">
<div class="left">Last 6 Months</div>
                                <div class="right"><img class="clip_image" src="http://media.monsterindia.com/v2/recruiter/2.1/new_search/newlook_combined.png"></div>
                                <div class="clear_both"></div>
                                </div></div>
<script language="javascript">  
                jQuery(document).ready(function(){  createSingleSelectCombo({id:'selDay',valueVariableName:'day',tabindex:'62',label:"",preSelected:"180",replaceWithId:'resume_freshness_container',width:'216',heightOptions:'height:240px;overflow-y:auto',animateScroll:true,
                options:[{id:'1',values:"in last 1 day"},
                {id:'3',values:"in last 3 days"},               
                {id:'7',values:"in last 7 days"},               
                {id:'15',values:"in last 15 days"},             
                {id:'30',values:"in last 1 month"},             
                {id:'90',values:"in last 3 months"},                
                {id:'180',values:"in last 6 months"},               
                {id:'360',values:"in last 12 months"},              
                {id:'540',values:"in last 18 months"},
                {id:'9999',values:"All Resumes"},
                {id:'4-7',values:"4-7 days"},
                {id:'8-15',values:"8-15 days"},
                {id:'16-30',values:"16-30 days"},
                {id:'31-90',values:"1-3 months"},
                {id:'91-180',values:"3-6 months"},
                {id:'181-360',values:"6-12 months"},
                {id:'361-540',values:"12-18 months"},
                {id:'541-9999',values:"Only older than 18 months"}

                ]});


                borderTopSingleSelect({container:'resume_freshness_container',afterId:'10'});

            });


</script>   

我使用以下代码选择下拉列表,然后选择所需选项:

Select select = new Select(driver.findElement(By.id("resume_freshness_container")));
select.deselectAll();
select.selectByVisibleText("All Resumes");

我也尝试用id“selDay”选择它,但是它给我的时间与我在下面列出的相同的例外

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"

*我是硒的新手,所以请帮助我知道我哪里出错*

1 个答案:

答案 0 :(得分:2)

是的,您无法使用Select类进行处理 - 它专门用于select->option HTML结构。

您需要首先找到id="selDay"元素,点击它,找到包含“所有简历”文本的元素,然后点击它:

WebElement selDay = driver.findElement(By.id("selDay"));
selDay.click();

WebElement allResumes = selDay.findElement(By.xpath("//*[.=\"All Resumes\"]"));
allResumes.click();