在此处尝试此示例以获取 JS 解决方法 - 作为 C# 扩展实现。此代码适用于 Safari(在 10.1+ 版本上测试)。
这不是完整的代码,只是一个使其简单的片段。您可以扩展它以支持您喜欢的任何功能。
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Support.UI;
namespace Gravity.Plugins.Actions.Extensions
{
public static class SelectExtensions
{
/// <summary>
/// Select the option by the index, as determined by the "index" attribute of the
/// element.
/// </summary>
/// <param name="selectElement">This <see cref="SelectElement"/>.</param>
/// <param name="index">The value of the index attribute of the option to be selected.</param>
public static void JsSelectByIndex(this SelectElement selectElement, int index)
{
// constants
var script = $"options[{index}].selected = true;";
// web element to act on
var onElement = selectElement.WrappedElement;
var onDriver = (IWrapsDriver)onElement;
// execute
((IJavaScriptExecutor)onDriver).ExecuteScript(script, onElement);
}
/// <summary>
/// Select all options by the text displayed.
/// </summary>
/// <param name="selectElement">This <see cref="SelectElement"/>.</param>
/// <param name="text">The text of the option to be selected.</param>
public static void JsSelectByText(this SelectElement selectElement, string text)
{
// constants
var script =
"var options = arguments[0].getElementsByTagName('option');" +
"" +
"for(i = 0; i < options.length; i++) {" +
$" if(options[i].innerText !== '{text}') {{" +
" continue;" +
" }" +
" options[i].selected = true;" +
" break;" +
"}";
// web element to act on
var onElement = selectElement.WrappedElement;
var onDriver = (IWrapsDriver)onElement;
// execute
((IJavaScriptExecutor)onDriver).ExecuteScript(script, onElement);
}
/// <summary>
/// Select an option by the value.
/// </summary>
/// <param name="selectElement"></param>
/// <param name="value">The value of the option to be selected.</param>
public static void JsSelectByValue(this SelectElement selectElement, string value)
{
// constants
var script =
"var options = arguments[0].getElementsByTagName('option');" +
"" +
"for(i = 0; i < options.length; i++) {" +
$" if(options[i].getAttribute('value') !== '{value}') {{" +
" continue;" +
" }" +
" options[i].selected = true;" +
" break;" +
"}";
// web element to act on
var onElement = selectElement.WrappedElement;
var onDriver = (IWrapsDriver)onElement;
// execute
((IJavaScriptExecutor)onDriver).ExecuteScript(script, onElement);
}
}
// Usage sample
public class MySeleniumClass
{
public void DoAutomation()
{
var driver = new ChromeDriver()
{
Url = "https://gravitymvctestapplication.azurewebsites.net/UiControls"
};
var element = driver.FindElement(By.Id("select_menu"));
var selectElement = new SelectElement(element);
selectElement.JsSelectByIndex(1);
selectElement.JsSelectByText("Two");
selectElement.JsSelectByValue("3");
}
}
}