arguments [0] .click()不工作在select选项在selenium [英] arguments[0].click() not working for select option in selenium

查看:4602
本文介绍了arguments [0] .click()不工作在select选项在selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用selenium进行网络应用程序自动化。

我停留在一点,我使用 .ExecuteScript()执行一些操作,像点击一个链接,为此使用: -

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')]")));


[注意:对于每个可点击的元素, ,这种方法,因为可点击的元素可能被隐藏或不可见的网页]
但是这种方法不适用于
< select> < option> item< option> ..< / select>


我使用以下代码单击选择选项之一:


[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]")));

但没有发生任何错误或异常。


- 编辑开始 -

但如果我使用 ExecuteScript()然后它的工作很好:

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();

- 编辑结束 -

< br> [注意:我使用点击选择选项,以便它触发更改事件。]

--Edit end--

[Note : I am using click to select options so that it fire the change event.]

使用((IJavaScriptExecutor)驱动程序)单击选择选项.ExecuteScript




提前感谢。

So can anyone please explain me how to click on the select option using ((IJavaScriptExecutor)driver).ExecuteScript

Thanks in advance.

推荐答案

对于下拉列表,您需要选择而不是点击。您应该返回元素,然后执行 element.SelectedIndex = 5;

For dropdowns you need to select and not click. You should return the element and then perform a element.SelectedIndex = 5;

如果需要修改javascript要通过javascript而不是selenium获取元素,您可以使用位于 document.evaluate /Web/API/document.evaluate?redirectlocale=en-US&redirectslug=DOM%2Fdocument.evaluaterel =nofollow> https://developer.mozilla.org/en-US/docs/Web/API/document .evaluate?redirectlocale = en-US& redirectslug = DOM%2Fdocument.evaluate

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.org/en-US/docs/Web/API/document.evaluate?redirectlocale=en-US&redirectslug=DOM%2Fdocument.evaluate

,因此您会传回代表select元素的元素, SelectedIndex 值。

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.com/Code/JavaScript/HTML/UsingthefireEventMethod.htm

这篇关于arguments [0] .click()不工作在select选项在selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆