如何创建JavaScript执行器,使元素在硒webdriver中可见 [英] How to create javascript executor to make element visible in selenium webdriver

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

问题描述

目前我正在研究硒webdriver。我有很多像可视化,期间,类型等下降。在下拉菜单中有很多选项。我想从下拉列表中选择一个选项,我的目标是通过ID查找元素。

但是在HTML标签中,选择该选项时该元素不可见。我验证了很多问题,他们提到使用javascript exceutor。

任何人都可以帮助我为html标记的java脚本:

 < select id =periodIdname =periodstyle =display:none;> 
< option value =l4w>过去4周< / option>
< option value =l52w>最近52周< / option>
< option value =daterange>日期范围< / option>
< option value =weekrange>周范围< / option>
< option selected =value =monthrange>月份范围< / option>
< option value =yeartodate> Year To Date< / option>
< / select>


解决方案

您可以尝试使用以下脚本来创建元素可见: document.getElementById('periodId')。style.display ='block';



这个脚本可以用下面的代码执行:

  JavascriptExecutor executor =(JavascriptExecutor)驱动程序; 
executor.executeScript(document.getElementById('periodId')。style.display ='block';);

如果您只想在下拉列表中选择一个选项,则可以使用以下java代码:

  Select select = new Select(driver.findElement(By.id(periodId))); 
select.deselectAll();
select.selectByVisibleText(过去4周);


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.

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.

Can any one please help me the java script for the html tag:

<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>

解决方案

You can try to use the following script to make element visible: document.getElementById('periodId').style.display='block';

In java code this script can be executed with the following code:

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:

Select select = new Select(driver.findElement(By.id("periodId")));
select.deselectAll();
select.selectByVisibleText("Last 4 Weeks");

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

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