最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - arguments[0].click() not working for select option in selenium - Stack Overflow

programmeradmin6浏览0评论

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.

Share Improve this question edited Sep 18, 2015 at 11:16 BhushanK asked Aug 13, 2014 at 15:23 BhushanKBhushanK 1,2436 gold badges24 silver badges39 bronze badges 6
  • Why are you doing it this why? Selenium has a SelectElement class that handles select elements and their options..... – 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 the SelectElement() 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
 |  Show 1 more ment

1 Answer 1

Reset to default 2

For 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

发布评论

评论列表(0)

  1. 暂无评论