Selenium 4 从下拉列表中选择时出错

发布于 2025-01-10 03:07:28 字数 200 浏览 1 评论 0原文

自从更新到 Selenium 4.1.2 以来,当使用所有选项从下拉列表中选择值时,我的测试失败:按值、索引和可见文本。请参阅下面的错误:

“JavaScriptException:无法单击选项元素。执行 Javascript 单击函数返回意外错误,但 Internet Explorer 的 JavaScript 引擎无法返回错误。”

有什么解决方法吗?

Since updating to Selenium 4.1.2, my tests fail when selecting a value from dropdown using all options: by value, index and visible text. See error below:

"JavaScriptException: Cannot click on option element. Executing Javascript click function returned an unexpected error, but no error could be returned from Internet Explorer's JavaScript engine."

Any workarounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2025-01-17 03:07:28

这似乎是各种版本的驱动程序中反复出现的错误。但是,您可以通过定义定义 Js 代码的方法来解决下拉列表管理问题。
本示例显示的是 C# 代码,但如果使用其他语言,原理是相同的。

public void SelectOption(By by, String value) 
        {
            Actions actions = new Actions(Driver);
            IWebElement wele = Driver.FindElement(by);
            IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)Driver;
            jsExecutor.ExecuteScript("arguments[0].click();", wele);
            jsExecutor.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text.toUpperCase() == arguments[1]){ select.options[i].selected = true; } }", wele, value);
            Thread.Sleep(TimeSpan.FromSeconds(1));                 
        }

如果下拉列表中有 onclick="" 或 onchage= "" 类型的函数,请记住在 js 代码中定义它们。
如下例所示,定义了select.onchange()

 public bool SelectOptionOnChange(By by, String value)
    {
        bool failed = true;
        Actions action = new Actions(Driver);
        if (!m_sHelper.boolWaitForElementIsDisplayed(by, TimeToWait.med))
            m_sHelper.SetErrorMsg("Elemento dropdownlist non identificato: " + value);
        else
        {
            IWebElement dropElement = Driver.FindElement(by);
            SelectElement dropOptions = new SelectElement(dropElement);
           
            if (dropOptions.Options.Count == 0)
                return true;
           
            try
            {
                IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)Driver;
                jsExecutor.ExecuteScript("arguments[0].click();", dropElement);
                jsExecutor.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text.toUpperCase() == arguments[1]){ select.options[i].selected = true; select.onchange();} }", dropElement, value);
                Thread.Sleep(TimeSpan.FromSeconds(1));                   
                failed = false;
            } catch (Exception)
            {
                failed = true;
            }
        }
        return failed;
    }

It seems to be a recurring error in various versions of the driver. However, you could work around the dropdownlist management issue by defining a method that defines Js code.
This example shows C# code, but the principle is the same if you use other languages

public void SelectOption(By by, String value) 
        {
            Actions actions = new Actions(Driver);
            IWebElement wele = Driver.FindElement(by);
            IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)Driver;
            jsExecutor.ExecuteScript("arguments[0].click();", wele);
            jsExecutor.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text.toUpperCase() == arguments[1]){ select.options[i].selected = true; } }", wele, value);
            Thread.Sleep(TimeSpan.FromSeconds(1));                 
        }

If on the dropdownlists are functions of the type onclick="" or onchage= "" remember to define them on the js code.
As in the following example, select.onchange() is defined.

 public bool SelectOptionOnChange(By by, String value)
    {
        bool failed = true;
        Actions action = new Actions(Driver);
        if (!m_sHelper.boolWaitForElementIsDisplayed(by, TimeToWait.med))
            m_sHelper.SetErrorMsg("Elemento dropdownlist non identificato: " + value);
        else
        {
            IWebElement dropElement = Driver.FindElement(by);
            SelectElement dropOptions = new SelectElement(dropElement);
           
            if (dropOptions.Options.Count == 0)
                return true;
           
            try
            {
                IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)Driver;
                jsExecutor.ExecuteScript("arguments[0].click();", dropElement);
                jsExecutor.ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text.toUpperCase() == arguments[1]){ select.options[i].selected = true; select.onchange();} }", dropElement, value);
                Thread.Sleep(TimeSpan.FromSeconds(1));                   
                failed = false;
            } catch (Exception)
            {
                failed = true;
            }
        }
        return failed;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文