【问题标题】:Dropdown not getting selected using Selenium Webdriver on Safari browser在 Safari 浏览器上使用 Selenium Webdriver 未选择下拉菜单
【发布时间】:2017-04-11 11:42:47
【问题描述】:

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

不会抛出任何错误,只是它没有从下拉列表中选择任何值。

【问题讨论】:

  • 是时间问题吗?也许 Mac 上的 Safari 很慢。在SelectElement 行上放置一个断点,然后单步执行。它有效吗?如果是这样,您需要添加一些等待。
  • 如果在你做完所有事情之后它仍然不起作用,那么使用 sendKeys 来选择可见文本怎么样。
  • @GaurangShah 试过了,但是下一行代码没有执行
  • @RohitN。不执行是什么意思?它跳过了吗?有没有抛出异常?
  • @Gaurang 无一例外只是跳过了代码执行,但这只会发生在 safari 浏览器上,其余的都可以正常工作,即使 Mac OS 上的 chrome 浏览器也可以正常运行

标签: c# selenium-webdriver safari webdriver safaridriver


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    在此处尝试此示例以获取 JS 解决方法 - 作为 C# 扩展实现。此代码适用于 Safari(在 10.1+ 版本上测试)。

    这不是完整的代码,只是一个简单的sn-p。您可以对其进行扩展以支持您喜欢的任何功能。

    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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 2017-10-29
      相关资源
      最近更新 更多