[英]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>
I am not able to change the value of the select box. 我无法更改选择框的值。 By default value 1 is selected.
默认情况下,值为1。 If I manually changed it to 5 then the following html code changes the value from 1 to 5.
如果我手动将其更改为5,则以下html代码会将值从1更改为5。
<span class="selectBox-label">5</span>
But the option tag attribute selected
hasn't changed. 但选择标签属性
selected
并没有改变。 The select tag is invisible. 选择标记不可见。
If following code is used then an exception appears. 如果使用以下代码,则会出现异常。
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
?
You mention that the select tag is invisble and that's why driver gives you the error that since it is not visible, you cannot interact with it. 您提到了select标记是不可见的,这就是为什么驱动程序给您的错误提示,因为它不可见,因此您无法与之交互。
Selenium used to allow interaction with hidden elements but not webdriver as it wants to more appropriately simulate user interaction. Selenium用于允许与隐藏元素进行交互,但不允许与webdriver交互,因为它希望更适当地模拟用户交互。
To achieve this in webdriver, you need to make the select tag visible first by doing the operation that makes it visible on the page and then use selectbylabel. 要在webdriver中实现此目的,您需要首先通过执行使其在页面上可见的操作来使select标记可见,然后使用selectbylabel。 Also your xpath seems to be considering the span id whereas select tag itself has an id to identify it.
同样,您的xpath似乎正在考虑span ID,而select标记本身具有一个ID来标识它。 I would suggest using select's id directly instead of going the relative way.
我建议直接使用select的ID,而不要采用相对方式。
Use This : 用这个 :
Select select=new Select(Utils.driver.findElement(By.xpath("//span[@id='outDuration']//select[@id='duration']")));
select.deselectAll();
select.selectByVisibleText("5");
If then too you get visibility exception then you would have to go with JavaScript Executor
. 如果那样的话,您也可以看到可见性异常,那么您就必须使用
JavaScript Executor
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.