1

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>
testing
  • 1,736
  • 15
  • 46
  • 75

1 Answers1

4

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");
ievche
  • 1,735
  • 14
  • 22
  • What is the language you are using? – ievche Dec 04 '13 at 10:48
  • Am using java and running scripts in selenium webdriver – testing Dec 04 '13 at 10:53
  • Thanks for your kind reply. i tried those code already the problem is the element is invisible and it is showing error as .ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 16 milliseconds. I need solution is there any other way to make element visible then i need to select the option. – testing Dec 04 '13 at 10:59
  • I used above code and i got the error as You may only deselect all options of a multi-select – testing Dec 04 '13 at 11:03
  • Try to remove `select.deselectAll();` row. – ievche Dec 04 '13 at 11:03
  • without giving the select.deselectAll(); am getting the errro as Element is not currently visible and so may not be interacted with Command duration or timeout: 16 milliseconds. – testing Dec 04 '13 at 11:08
  • Add code with javascript executor from my answer, to make element visible. – ievche Dec 04 '13 at 11:09
  • Thanks now i can select the element from the drop down. – testing Dec 04 '13 at 11:19
  • 1
    Also you can mark question as correct, if it is really correct. Or click 'up' row near the question, if it was helpful. This is the way to say thanks for help! – ievche Dec 04 '13 at 11:40