如何使元素可见在硒的webdriver? [英] How to make element visible in selenium webdriver?

查看:157
本文介绍了如何使元素可见在硒的webdriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我如何从下拉列表中选择选项,如果元素不可见,它是一个布尔属性。这里是html标签:

Please anyone can help me how to select options from the drop down if the element is not visible and it is a boolean attribute. Here is the html tag:

<select id="visualizationId" style="width: 120px; display: none;" name="visualization">
<option value="day">Day</option>
<option value="week">Week</option>
<option selected="" value="month">Month</option>

我在selenium webdriver上工作。下面的代码不工作正常。是否有任何示例代码来选择不可见元素。

Am working on selenium webdriver. the below code is not working fine. Is there any sample code to select the invisible element.

Actions actions1 = new Actions(driver);
WebElement dBox1= ((new    WebDriverWait(driver,60)).until(ExpectedConditions.elementToBeClickable(By.id("visualizationId"))));
selectByVisibleText("week");
actions1.moveToElement(dBox1);
actions1.click();
actions1.perform();

当使用下面的行也得到错误:元素当前不可见,因此可能不是与命令持续时间或超时交互:32毫秒

When using the below lines also am getting the error: Element is not currently visible and so may not be interacted with Command duration or timeout: 32 millisecond

Select se=new Select(driver.findElement(By.id("visualizationId")));
se.selectByVisibleText("Week");

se.selectByValue("week");

请看html,那里的元素不可见。可以任何一个建议我如何使元素可见,然后如何选择选项。

Please see the html and there the element is not visible. can any one suggest me how to make element visible then how can i select the option.

推荐答案

在selenium中使用本机API。像webElement.click()这样的函数适用于用户将能够在浏览器窗口中处理的元素,即可见元素。

You have to be careful when you use native APIs in selenium. Things like webElement.click() and such apply to elements that user would be able to work with in the browser window, namely visible elements.

如果下拉不是可见,您不能使用原生API与其进行交互。

If a drop down is not visible, you cannot use native APIs to interact with them.

您可以尝试使用javascript执行程序。

You can try to use the javascript executor.

你最有可能通过执行以下操作来破解你的select元素来获得你想要的值:
jsExecutor.executeScript(String.format(arguments [0] .value ='%1 $ s' ;,valueToSet),selectWebElement);

You most likely will be able to hack your select element to get the value you want by doing something such as: jsExecutor.executeScript(String.format("arguments[0].value = '%1$s';", valueToSet ), selectWebElement);

但是如果你这样做,你不是真的测试UI。

But if you do this, you're not really testing the UI.

否则,你也可以尝试通过做style.diyplay ='block'(假设父元素可见)使其可见。

Otherwise, you can also try to make it visible by doing style.diyplay = 'block' (assuming the parent element is visible).

UI应该以某种方式给你一些自然的方式使该选择框可见。你使用主面组件或这样的小部件隐藏输入元素和渲染,而不是漂亮的div来模拟下拉等等?如果是,与虚拟小部件交互,而不是与原生输入元素交互。

However, your UI should somehow give you some sort of natural way to make that select box visible. Are you using prime faces components or such widgets that hide away the input elements and render instead "pretty" divs to simulate the drop downs and such? If so, interact with the dummy widgets instead of with the native input elements.

这篇关于如何使元素可见在硒的webdriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆