【问题标题】:Unable to select (click) option in dropdown无法在下拉列表中选择(单击)选项
【发布时间】:2015-11-13 17:02:53
【问题描述】:

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

这是我用来选择并单击第一个和第二个下拉菜单的代码。第一个下拉列表是前三行。然后我插入一个等待以等待“加载”弹出窗口消散。 然后第二个下拉菜单的另外三行与第一行几乎相同,另一行在单击按钮之前等待。

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>

【问题讨论】:

    标签: java eclipse selenium


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      相关资源
      最近更新 更多