简体   繁体   中英

Select an option from hidden drop down in selenium

Hey guys I'm having this problem where I have a hidden select drop down where its style set to none.

<div>
<select id="selectForm" style="width: 285px; display: none;" name="formId">
<option value="36784">EMAIL 4</option>
</select>
<div class="sbox sbox_1cbc5b38 selectForm" tabindex="0" style="width: 285px;">
<div class="sbox_sel">
<div class="sbox_seltext" contenteditable="false">Report</div>
<div class="sbox_arrow"/>
</div>
</div>
<a id="previewForm" class="preview-button"/>
</div>

I was getting element not visible when I was looking By.id for the select tag. And the same error for when I was trying to click on the div. I found a way to make this drop down visible with jsExecutor. Here is my code to make this drop down visible to interact with:

public static void BatchResults(String form){

WebDriverWait wait = new WebDriverWait(driver, 60);
JavascriptExecutor js = (JavascriptExecutor)driver;                                         
js.executeScript("document.getElementById('selectForm').style.display='block';");
WebElement selectFormElement =                    
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("selectForm")));
Select selectForm = new Select(selectFormElement);  
selectForm.selectByVisibleText(form);
js.executeScript("document.getElementById('selectForm').style.display='none';");  

}

@Test(priority = 1)
public void runTest() {
    String selectForm = "EMAIL 4";
    Main.BatchResults(selectForm);
}
}

My issue is that, in jsExecutor I am setting the style to (style.display='block';) and it is bringing my underlining drop down on top of the styled drop down. Is there way to select this hidden drop down without using jsExecutor or what other option should I use? Also I cannot still select the option I want from the drop down after it is set to block and then none after selection

<ul class="sbox_opts sbox_fa9ea48e" style="width: 283px; top: 610.5px; 
left: 330.333px; max-height: 54.5px; display: none;">
<li class="sbox_opt">EMAIL 4</li>

</ul>

Selenium is designed to navigate web pages the same way a user would. You should have a visible element that when interacted with (hover/click) will display the drop down. Once the drop down is displayed you can interact with the contents.
Have Selenium perform the same tasks a user would have to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM