英文:
Problem with dropdown list working with Selenium
问题
尝试从下拉列表中选择一个元素,但在执行过程中出现了以下错误:
> 主线程中的异常"org.openqa.selenium.NoSuchElementException:
> 没有这样的元素:无法定位元素:
> {"method":"xpath","selector":"//select[@id='id12']"}
下拉列表的HTML代码如下:
```html
<select style="width:163px" name="department:department" id="id12">
<option selected="selected" value="">Escoge</option>
<option value="100">100</option>
<option value="999">999</option>
<option value="800">800</option>
<option value="700">700</option>
<option value="600">600</option>
<option value="540">540</option>
<option value="500">500</option>
<option value="400">400</option>
<option value="345">345</option>
<option value="280">280</option>
<option value="270">270</option>
<option value="264">264</option>
<option value="262">262</option>
<option value="251">251</option>
<option value="201">201</option>
<option value="82">82</option>
<option value="81">81</option>
<option value="50">50</option>
<option value="21">21</option>
<option value="19">19</option>
<option value="001">001</option>
</select>
我的Java代码如下:
WebElement dropdownlist = driver.findElement(By.xpath("//select[@id='id12']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
我也尝试了以下代码:
WebElement dropdownlist = driver.findElement(By.xpath("//select[@name='department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
WebElement dropdownlist = driver.findElement(By.cssSelector("select[id='id12']"));
<details>
<summary>英文:</summary>
I'm trying to select an element from a dropdown list, but I got this error in the execution:
> Exception in thread "main" org.openqa.selenium.NoSuchElementException:
> no such element: Unable to locate element:
> {"method":"xpath","selector":"//select[@id='id12']"}
The HTML code of the dropdown list is:
```html
<select style="width:163px" name="department:department" id="id12">
<option selected="selected" value="">Escoge</option>
<option value="100">100</option>
<option value="999">999</option>
<option value="800">800</option>
<option value="700">700</option>
<option value="600">600</option>
<option value="540">540</option>
<option value="500">500</option>
<option value="400">400</option>
<option value="345">345</option>
<option value="280">280</option>
<option value="270">270</option>
<option value="264">264</option>
<option value="262">262</option>
<option value="251">251</option>
<option value="201">201</option>
<option value="82">82</option>
<option value="81">81</option>
<option value="50">50</option>
<option value="21">21</option>
<option value="19">19</option>
<option value="001">001</option>
</select>
My java code is:
WebElement dropdownlist = driver.findElement(By.xpath("//select[@id='id12']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
I've tried with this too:
WebElement dropdownlist = driver.findElement(By.xpath("//select[@name='department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
WebElement dropdownlist = driver.findElement(By.cssSelector("select[id='id12']"));
答案1
得分: 0
可能这里 id 是动态的,你提供的 name 属性值是错误的。
尝试以下代码。
WebElement dropdownlist = driver.findElement(By.xpath("//select[@name='department:department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
注意:上述代码中的定位字符串是根据原始内容翻译的,实际使用时请根据实际情况进行确认。
英文:
Possibly id is dynamic here and name attribute value you have provided is wrong.
Try the below code.
WebElement dropdownlist = driver.findElement(By.xpath("//select[@name='department:department']"));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText("100");
答案2
得分: 0
要处理下拉框,你需要使用org.openqa.selenium.support.ui.Select
导入,并将下拉框实例化为WebDriver中的"Select"对象。同时,可能是因为您尝试执行操作的元素需要时间,因此出现了异常。添加显式等待并从您的端口检查::
WebElement element = driver.findElement(By.id("id12"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
Select dropdown = new Select(element);
dropdown.selectByVisibleText("100");
英文:
To deal with dropdown you have to use org.openqa.selenium.support.ui.Select
import and it will nstantiate the drop-down box as a "Select" object in WebDriver. Also it could be possible that element on which you are trying to perform action taking time and hence the exception occurred. Add Explicit wait and check from your end ::
WebElement element=driver.findElement(By.id("id12"));
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
Select dropdown = new Select(element);
dropdown.selectByVisibleText("100");
专注分享java语言的经验与见解,让所有开发者获益!
评论