如何创建 javascript 执行程序以使元素在 selenium webdriver 中可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20371077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create javascript executor to make element visible in selenium webdriver
提问by testing
Currently am working on selenium webdriver. I have many drop downs like visualization, Period, Type etc,. In the drop down many options are there. I want to select an option from the drop down and my target is to find element is through ID.
目前正在研究 selenium webdriver。我有很多下拉菜单,如可视化、周期、类型等。在下拉菜单中有许多选项。我想从下拉列表中选择一个选项,我的目标是通过 ID 查找元素。
But in the HTML tag the element is not visible to select the option. I verified so many question in that they are mentioning use javascript exceutor.
但是在 HTML 标记中,该元素不可见以选择该选项。我验证了很多问题,因为他们提到了使用 javascript executor。
Can any one please help me the java script for the html tag:
任何人都可以帮助我 html 标签的 java 脚本:
<select id="periodId" name="period" style="display: none;">
<option value="l4w">Last 4 Weeks</option>
<option value="l52w">Last 52 Weeks</option>
<option value="daterange">Date Range</option>
<option value="weekrange">Week Range</option>
<option selected="" value="monthrange">Month Range</option>
<option value="yeartodate">Year To Date</option>
</select>
回答by Ievgen
You can try to use the following script to make element visible: document.getElementById('periodId').style.display='block';
您可以尝试使用以下脚本使元素可见: document.getElementById('periodId').style.display='block';
In java code this script can be executed with the following code:
在 java 代码中,可以使用以下代码执行此脚本:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('periodId').style.display='block';");
If you just want to select an option in drop down you can use the following java code:
如果您只想在下拉列表中选择一个选项,您可以使用以下 java 代码:
Select select = new Select(driver.findElement(By.id("periodId")));
select.deselectAll();
select.selectByVisibleText("Last 4 Weeks");