C#:使用 Marionette 驱动程序选择下拉项
C#: Selecting Dropdown Items with Marionette Driver
我正在使用 Selenium Webdriver
和 C#
绑定并尝试从旧的 FirefoxDriver(FF 47 之前)切换到新的 Marionette driver
(FF47 及更高版本)并且它正在工作在 Selenium 2.53.1
和 FF 47.0.1
的发布后似乎解决了一些初期问题后非常棒。
现在唯一的问题是 select 在 select 标签下的选项标签似乎有问题。以下代码适用于我正在测试的所有其他浏览器(FF < 46、Chrome、IE)。我将以下参数传递到我的 dropdownSelect
函数中。 select IWebElement
和要搜索的文本。这是函数定义:
public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText)
我试过使用 SelectElement()
class 就像我在所有其他浏览器中一样
select = new SelectElement(inObject);
//select the matching element
select.SelectByText(inText);
我也尝试过获取选项的集合并使用 Click()
:
滚动浏览集合
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
ReadOnlyCollection<IWebElement> optDropdown;
optDropdown = inObject.FindElements(By.TagName("option"));
foreach (IWebElement thsItem in optDropdown)
{
//check for matching text
if (thsItem.Text == inText)
{
// 1/4 second wait
Thread.Sleep(250);
thsItem.Click()
//exit foreach loop
break;
}
}
和 javascript
单击代替 thsItem.Click()
代码段
//click option element
js.ExecuteScript("arguments[0].click();", thsItem);
什么都不会 selected,也不会抛出任何错误或异常。它只是继续它的快乐方式,没有 selecting 任何东西
我是不是做错了什么,还是新的 Marionette 驱动程序仍在解决这个问题?
尝试使用 ExecuteScript()
进行整个操作,如下所示:-
public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText) {
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", inObject, inText);
}
希望它能奏效...:)
我通过使用与上述类似的 Javascript 解决了这个问题。由于此下拉列表在更改时存在依赖性,因此我只是 select 在 Selenium 中找到适当的选项并触发 onchange 即使 Javascript
这是 select 框的 HTML
<select class="T2FormControl" id="ctl00_pageContent_TableList_T2DropDownList_DropDownList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$pageContent$TableList$T2DropDownList$DropDownList\',\'\')', 0)" name="ctl00$pageContent$TableList$T2DropDownList$DropDownList">
以及执行动作的Javascript
//click option element and for change event
js.ExecuteScript("arguments[0].selected = true;" +
"var element=arguments[1];" +
"var event=document.createEvent(\"HTMLEvents\");" +
"event.initEvent('change', false, true);" +
"element.dispatchEvent(event);", thsItem, inObject);
IWebElement thsItem 是选项 selected,IWebElement inObject 是下拉菜单的 select 标签
其他 Selenium 驱动程序自动执行的操作似乎是一种迂回的方式,但它确实有效
我正在使用 Selenium Webdriver
和 C#
绑定并尝试从旧的 FirefoxDriver(FF 47 之前)切换到新的 Marionette driver
(FF47 及更高版本)并且它正在工作在 Selenium 2.53.1
和 FF 47.0.1
的发布后似乎解决了一些初期问题后非常棒。
现在唯一的问题是 select 在 select 标签下的选项标签似乎有问题。以下代码适用于我正在测试的所有其他浏览器(FF < 46、Chrome、IE)。我将以下参数传递到我的 dropdownSelect
函数中。 select IWebElement
和要搜索的文本。这是函数定义:
public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText)
我试过使用 SelectElement()
class 就像我在所有其他浏览器中一样
select = new SelectElement(inObject);
//select the matching element
select.SelectByText(inText);
我也尝试过获取选项的集合并使用 Click()
:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
ReadOnlyCollection<IWebElement> optDropdown;
optDropdown = inObject.FindElements(By.TagName("option"));
foreach (IWebElement thsItem in optDropdown)
{
//check for matching text
if (thsItem.Text == inText)
{
// 1/4 second wait
Thread.Sleep(250);
thsItem.Click()
//exit foreach loop
break;
}
}
和 javascript
单击代替 thsItem.Click()
代码段
//click option element
js.ExecuteScript("arguments[0].click();", thsItem);
什么都不会 selected,也不会抛出任何错误或异常。它只是继续它的快乐方式,没有 selecting 任何东西
我是不是做错了什么,还是新的 Marionette 驱动程序仍在解决这个问题?
尝试使用 ExecuteScript()
进行整个操作,如下所示:-
public static void dropdownSelect(IWebDriver driver, IWebElement inObject, string inText) {
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", inObject, inText);
}
希望它能奏效...:)
我通过使用与上述类似的 Javascript 解决了这个问题。由于此下拉列表在更改时存在依赖性,因此我只是 select 在 Selenium 中找到适当的选项并触发 onchange 即使 Javascript
这是 select 框的 HTML
<select class="T2FormControl" id="ctl00_pageContent_TableList_T2DropDownList_DropDownList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$pageContent$TableList$T2DropDownList$DropDownList\',\'\')', 0)" name="ctl00$pageContent$TableList$T2DropDownList$DropDownList">
以及执行动作的Javascript
//click option element and for change event
js.ExecuteScript("arguments[0].selected = true;" +
"var element=arguments[1];" +
"var event=document.createEvent(\"HTMLEvents\");" +
"event.initEvent('change', false, true);" +
"element.dispatchEvent(event);", thsItem, inObject);
IWebElement thsItem 是选项 selected,IWebElement inObject 是下拉菜单的 select 标签
其他 Selenium 驱动程序自动执行的操作似乎是一种迂回的方式,但它确实有效