无法在下拉列表中选择(单击)选项

时间:2015-11-12 12:48:41

标签: java eclipse selenium

我有一种情况,我必须从下拉列表(复杂和对象)中选择2个值,然后单击按钮才能获得报告。我可以在第一个下拉列表(复杂)中选择一个没有问题的值但是当我尝试在第二个下拉列表(对象)中选择一个选项时,它只是不选择该选项并继续单击按钮。这是在脚本单击按钮之前的情况屏幕截图。正如您所看到的,它为第一个下拉列表选择了正确的选项,看起来它找到了第二个下拉列表的正确选项,但没有点击它:

enter image description here

以下是我用来选择adn的代码,点击第一个和第二个下拉列表。第一个下拉列表是前三行。然后我插入等待以等待加载' popup to dissaper。 然后第二个下拉列表中的另外三行与第一行几乎相同,另一行在单击按钮之前等待。

driver.findElement(By.id("dropdown1")).click();
new Select(driver.findElement(By.id("dropdown1"))).selectByVisibleText("31H-135");
driver.findElement(By.cssSelector("option[value=\"1466\"]")).click(); //there are a lot of options here and the one I need is 1466
try {
    Thread.sleep(3000);                 //3000 milliseconds is three second wait for loading popup.
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
driver.findElement(By.id("Dropdown2")).click();
new Select(driver.findElement(By.id("Dropdown2"))).selectByVisibleText("31H-135-01");
driver.findElement(By.xpath("/html/body/div/form/div[3]/span/div/table/tbody/tr[1]/td/div/div/table/tbody/tr/td[1]/table/tbody/tr/td[5]/div/select/option[2]")).click();
try {
    Thread.sleep(3000);                 //3000 milliseconds is three second wait for loading popup.
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

driver.findElement(By.id("ReportViewer1_ctl04_ctl00")).click();   //view report button  

这里有第一个和第二个下拉列表的一些代码:

第一次下拉:

<select id="Dropdown1" style="font-family: Verdana; font-size: 8pt; width: 127px;" onchange="javascript:setTimeout('__doPostBack(\'ReportViewer1$ctl04$ctl03$ddValue\',\'\')', 0)" name="ReportViewer1$ctl04$ctl03$ddValue">
<option value="1">02C-377</option>
<option value="2">02C-378</option>
<option value="3">02G-375</option>
<option value="4">02G-376</option>
<option value="5">03G-100</option>
<option value="6">03G-101</option>
<option value="7">03G-102</option>
<option value="8">03G-103</option>
<option value="9">03G-104</option>

第二个下拉列表(值基于第一个下拉列表):

<select id="Dropdown2" style="font-family:Verdana;font-size:8pt;" name="ReportViewer1$ctl04$ctl05$ddValue">
<option value="0" selected="selected"><Select a Value></option>
<option value="1">31H-135-01</option>
</select>

1 个答案:

答案 0 :(得分:0)

尝试按值选择或使用绝对xpath来识别元素。

相关问题