繁体   English   中英

无法使用Java / webDriver中的选择类更改选择框值

[英]Not able to change select box value using select class in Java/webDriver

<span id="outDuration" class="check_duration last flL" onclick="hideCalendar('#pickUpDate');">
      <label style="width:95px">
      <span class="Class-Outmatch">
             <select id="duration" class="selectBox"  tabindex="7" name="duration">
                         <option selected="selected" value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                         <option value="4">4</option>
                         <option value="5">5</option>
                         <option value="6">6</option>
                         <option value="7">7</option>
                         <option value="8">8</option>
                         <option value="9">9</option>
            </select>
            <span class="left_part flL firefinder-match"></span>
            <span class="selectBox center_part flL selectBox-dropdown" tabindex="7">
                         <span class="selectBox-label">5</span>

我无法更改选择框的值。 默认情况下,值为1。 如果我手动将其更改为5,则以下html代码会将值从1更改为5。

<span class="selectBox-label">5</span> 

但选择标签属性selected并没有改变。 选择标记不可见。

如果使用以下代码,则会出现异常。

Select select = driver.findElement(By.xpath("//span[@id='outDuration']/span/select")).SelectByVisibleText("5");

Exception: Element is not currently visible and so may not be interacted with

您是否尝试过使用SelectByValue而不是SelectByVisibleText

您提到了select标记是不可见的,这就是为什么驱动程序给您的错误提示,因为它不可见,因此您无法与之交互。

Selenium用于允许与隐藏元素进行交互,但不允许与webdriver交互,因为它希望更适当地模拟用户交互。

要在webdriver中实现此目的,您需要首先通过执行使其在页面上可见的操作来使select标记可见,然后使用selectbylabel。 同样,您的xpath似乎正在考虑span ID,而select标记本身具有一个ID来标识它。 我建议直接使用select的ID,而不要采用相对方式。

用这个 :

Select select=new Select(Utils.driver.findElement(By.xpath("//span[@id='outDuration']//select[@id='duration']")));
select.deselectAll();
select.selectByVisibleText("5");

如果那样的话,您也可以看到可见性异常,那么您就必须使用JavaScript Executor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM