I am trying to click a button on a webpage with WebDriver which uses JavaScript to expand a menu revealing other 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
I have tried using methods suggested on this site including Xpath and CSS
Browser.Driver.FindElement(By.XPath("//div[@id='advanced_bt']/b/span")).Click();
or
Browser.Driver.FindElement(By.CssSelector("#advanced_bt > b > span")).Click();
and also tried with IJavaScriptExecutor:
var executor = (IJavaScriptExecutor)webDriver;
executor.ExecuteScript("arguments[0].click();", element);
Neither have worked. The test does not fail to locate the elements, so I'm out of ideas why the can't be clicked. If anyone can shed any light that would be appreciated. Cheers.
Oh and this is the onclick= code:
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();
}