I am using selenium for the web application automation.
I stuck in one point,I am using .ExecuteScript()
to perform some action like to click on a link and for that am using :-
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//a[contains(text(),'Login to the Demo')]")));
[Note : for every click-able element am using ,this approach because click-able element may be hidden or not visible in web page]
But this approach is not working for
<select> <option>item<option> .. </select>
I am using below code clicking on one of the select option :
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")));
but nothing is happening nor giving any error/exception.
--Edit start--
But if I use without ExecuteScript()
then its work fine:
driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")).Click();
--Edit end--
[Note : I am using click to select options so that it fire the change event.]
So can anyone please explain me how to click on the select option using ((IJavaScriptExecutor)driver).ExecuteScript
Thanks in advance.
I am using selenium for the web application automation.
I stuck in one point,I am using .ExecuteScript()
to perform some action like to click on a link and for that am using :-
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//a[contains(text(),'Login to the Demo')]")));
[Note : for every click-able element am using ,this approach because click-able element may be hidden or not visible in web page]
But this approach is not working for
<select> <option>item<option> .. </select>
I am using below code clicking on one of the select option :
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")));
but nothing is happening nor giving any error/exception.
--Edit start--
But if I use without ExecuteScript()
then its work fine:
driver.FindElement(By.XPath("//select[@id='form_switcher']/option[5]")).Click();
--Edit end--
[Note : I am using click to select options so that it fire the change event.]
So can anyone please explain me how to click on the select option using ((IJavaScriptExecutor)driver).ExecuteScript
Thanks in advance.
-
Why are you doing it this why? Selenium has a
SelectElement
class that handlesselect
elements and theiroptions
..... – Arran Commented Aug 13, 2014 at 20:46 -
@Arran : but when dropdown is not visible (consider
select
is get visible on mouseover on some menu) then it will throw exception. – BhushanK Commented Aug 14, 2014 at 4:08 -
If the entire
<select>
element is hidden how would the user interact with it as normal? Do you mean the<option>
of the<select>
is hidden? If yes, then use theSelectElement()
class to handle it as normal. – Mark Rowlands Commented Aug 14, 2014 at 8:04 -
@MarkRowlands : the entier
<select>
element is on a<div>
which get visible on mouseover only. – BhushanK Commented Aug 14, 2014 at 8:40 -
1
You'll likely have to use
ActionChains()
to 'mouse over' the<div>
before you can 'see' and interact with your<select>
element. – Mark Rowlands Commented Aug 14, 2014 at 10:47
1 Answer
Reset to default 2For dropdowns you need to select and not click. You should return the element and then perform a element.SelectedIndex = 5;
If you need to modify your javascript to get the element via javascript instead of selenium you can utilize the document.evaluate
located https://developer.mozilla/en-US/docs/Web/API/document.evaluate?redirectlocale=en-US&redirectslug=DOM%2Fdocument.evaluate
so then you return an element that represents your select element and then set the SelectedIndex
value.
I believe this is correct...
((IJavaScriptExecutor)driver).ExecuteScript("var element = document.evaluate(\"//select[@id='form_switcher']\", document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); element.SelectedIndex = 5; return element.fireEvent('event specifics go here')");
http://www.java2s./Code/JavaScript/HTML/UsingthefireEventMethod.htm