I'm currently utilizing Selenium for automating web applications. I've encountered an issue where I am utilizing .ExecuteScript()
to carry out actions such as clicking on a link, with the following syntax:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//a[contains(text(),'Login to the Demo')]")));
[Note: I use this approach for every clickable element, as the element may be hidden or not visible on the webpage.] However, this method does not work when dealing with
<select> <option>item<option> .. </select>
I attempted to click on one of the select options using the code below:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")));
No action is being taken and no error/exception is thrown.
--Edit start--
However, when I try without using ExecuteScript()
, it works perfectly fine:
driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")).Click();
--Edit end--
[Note: I am using click to select options in order to trigger the change event.]
If anyone could provide guidance on how to click on a select option using
((IJavaScriptExecutor)driver).ExecuteScript
, it would be greatly appreciated.
Thank you in advance.