How to create a Javascript executor for making an element visible in Selenium Webdriver

0 votes

Currently, I am working on Selenium Webdriver. I have many dropdowns like visualization, period, type, etc. These dropdowns contain many options and I want to select an option from it and my target is to find an element through its ID.

But, in the HTML tag, the element is not visible to select an option. I tried verifying by putting across a lot of questions and most of them are suggesting to use the Javascript executor.

Can anyone please help me the javascript 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>
Mar 26, 2019 in Selenium by Vaishnavi
• 1,180 points
8,324 views

2 answers to this question.

0 votes

Hey there,
You can try using the following script to make an element visible: 

document.getElementById('periodId').style.display='block';

In Java code this script can be executed with the following snippet:

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('periodId').style.display='block';");

And if you just want to select an option in the dropdown you can use the following java code:

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

Hope this is helpful.

answered Mar 26, 2019 by Surya
• 970 points
+1 vote
document.getElementById("myBtn").disabled = true;
answered Aug 31, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers

How to verify color of a web element in Selenium Webdriver?

document.getElementsByTagName('div')[0].style.backgroundColor READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
17,550 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,567 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,084 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,178 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,840 views
0 votes
2 answers

How can we use JavaScript Executor to click and enter data to a web element in Selenium?

WebElement element = driver.findElement(By.id("abcd")); // Let the ...READ MORE

answered Mar 22, 2020 in Selenium by Lakshmi Sarvepalli
3,437 views
0 votes
5 answers

How to use JavaScript in selenium to click an Element?

WebElement element = driver.findElement(By.id("id")); JavascriptExe ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
70,278 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP