4

在 Safari 浏览器上,我需要从下拉列表中选择一个选项,但有趣的是它适用于除 Mac OS 上的 Safari 之外的所有浏览器。我正在使用 Safari 10.0.3 和 selenium webdriver 版本 3.3.0

我已经用 C# 编写了代码。请参阅下面的代码 -

    IWebDriver driver;
    driver = new SafariDriver();
    List<string> handles = driver.WindowHandles.ToList<string>();
    driver.SwitchTo().Window(handles.First());
    driver.Navigate().GoToUrl("https://myip/MyPage.aspx");
    SelectElement element = new SelectElement(driver.FindElement(By.Id("securityQuestion")));
    int totalOptions = element.Options.Count;
    Random rnd = new Random();
    int rndValue = rnd.Next(1, totalOptions);
    element.SelectByIndex(rndValue); // This is not working for Safari browser      
    driver.FindElement(By.Id("securityAnswer")).SendKeys("test");
    driver.FindElement(By.Id("ctl00_Content_btnNext")).Click();
    driver.Close();

没有错误只是因为它没有从下拉列表中选择任何值。

4

2 回答 2

0

这是一个 safaridriver 错误。修复在 WebKit 中,并在此处进行跟踪: https ://bugs.webkit.org/show_bug.cgi?id=174710

作为一种解决方法,您可以使用 JavaScript 和 DOM API 修改选择的哪些选项。

于 2017-08-10T19:23:09.117 回答
0

在此处尝试此示例以获取 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");
        }
    }
}
于 2020-06-06T21:48:41.557 回答