使用Selenium工作的下拉列表出现问题。

huangapple 未分类评论44阅读模式
英文:

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&#39;m trying to select an element from a dropdown list, but I got this error in the execution:

&gt; Exception in thread &quot;main&quot; org.openqa.selenium.NoSuchElementException:
&gt; no such element: Unable to locate element:
&gt; {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//select[@id=&#39;id12&#39;]&quot;}

The HTML code of the dropdown list is: 
```html
&lt;select style=&quot;width:163px&quot; name=&quot;department:department&quot; id=&quot;id12&quot;&gt;
    &lt;option selected=&quot;selected&quot; value=&quot;&quot;&gt;Escoge&lt;/option&gt;
    &lt;option value=&quot;100&quot;&gt;100&lt;/option&gt;
    &lt;option value=&quot;999&quot;&gt;999&lt;/option&gt;
    &lt;option value=&quot;800&quot;&gt;800&lt;/option&gt;
    &lt;option value=&quot;700&quot;&gt;700&lt;/option&gt;
    &lt;option value=&quot;600&quot;&gt;600&lt;/option&gt;
    &lt;option value=&quot;540&quot;&gt;540&lt;/option&gt;
    &lt;option value=&quot;500&quot;&gt;500&lt;/option&gt;
    &lt;option value=&quot;400&quot;&gt;400&lt;/option&gt;
    &lt;option value=&quot;345&quot;&gt;345&lt;/option&gt;
    &lt;option value=&quot;280&quot;&gt;280&lt;/option&gt;
    &lt;option value=&quot;270&quot;&gt;270&lt;/option&gt;
    &lt;option value=&quot;264&quot;&gt;264&lt;/option&gt;
    &lt;option value=&quot;262&quot;&gt;262&lt;/option&gt;
    &lt;option value=&quot;251&quot;&gt;251&lt;/option&gt;
    &lt;option value=&quot;201&quot;&gt;201&lt;/option&gt;
    &lt;option value=&quot;82&quot;&gt;82&lt;/option&gt;
    &lt;option value=&quot;81&quot;&gt;81&lt;/option&gt;
    &lt;option value=&quot;50&quot;&gt;50&lt;/option&gt;
    &lt;option value=&quot;21&quot;&gt;21&lt;/option&gt;
    &lt;option value=&quot;19&quot;&gt;19&lt;/option&gt;
    &lt;option value=&quot;001&quot;&gt;001&lt;/option&gt;
&lt;/select&gt;

My java code is:

WebElement dropdownlist = driver.findElement(By.xpath(&quot;//select[@id=&#39;id12&#39;]&quot;));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText(&quot;100&quot;);

I've tried with this too:

WebElement dropdownlist = driver.findElement(By.xpath(&quot;//select[@name=&#39;department&#39;]&quot;));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText(&quot;100&quot;);

WebElement dropdownlist = driver.findElement(By.cssSelector(&quot;select[id=&#39;id12&#39;]&quot;));

答案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(&quot;//select[@name=&#39;department:department&#39;]&quot;));
Select dropdown = new Select(dropdownlist);
dropdown.selectByVisibleText(&quot;100&quot;);

答案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.Selectimport 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(&quot;id12&quot;));
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
Select dropdown = new Select(element);
dropdown.selectByVisibleText(&quot;100&quot;);

huangapple
  • 本文由 发表于 2020年4月4日 04:44:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61020126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定