I am facing a challenge in clicking a button on a website using WebDriver that utilizes JavaScript to expand a menu and display additional links:
<div id="menu">
<div id="security_bt" class="advanced_white_close_button" onclick="open_or_close_sub('security');
security_open++;"><b> <span languageCode = "13">Security</span></b></div>
<div id="advanced_bt" class="advanced_white_close_button" onclick="open_or_close_sub('advanced');
advanced_open++;"><b><span languageCode = "3011">Advanced Setup</span></b></div>
...etc
Several methods have been attempted, such as Xpath and CSS selectors, with no success.
Browser.Driver.FindElement(By.XPath("//div[@id='advanced_bt']/b/span")).Click();
or
Browser.Driver.FindElement(By.CssSelector("#advanced_bt > b > span")).Click();
An attempt was made using IJavaScriptExecutor:
var executor = (IJavaScriptExecutor)webDriver;
executor.ExecuteScript("arguments[0].click();", element);
Despite the elements being located successfully, they cannot be clicked. Any insight would be greatly appreciated. Thank you.
The following is the onclick= code snippet:
function open_or_close_sub(name)
{
var button_name= name+"_bt";
var sub_name= name+"_sub";
var open_flag= top.document.getElementById(sub_name).style.display;
close_all_sub(sub_name);/* fold all menus first, except the menu which user click*/
var button_div = top.document.getElementById(button_name);
var content_length = button_div.getElementsByTagName("span")[0].innerHTML.length;
if( open_flag == "none")
{
settingClass(button_div, content_length, "advanced_white_open_button", top.region_class.white_triple, top.region_class.white_double);
top.document.getElementById(sub_name).style.display="";
}
else
{
settingClass(button_div, content_length, "advanced_white_close_button", top.region_class.white_triple, top.region_class.white_double);
top.document.getElementById(sub_name).style.display="none";
}
change_menu_height();
}