【问题标题】:Can't Select drop down value: jQuery, C#, and Selenium无法选择下拉值:jQuery、C# 和 Selenium
【发布时间】:2017-05-29 02:46:41
【问题描述】:

我正在尝试在下拉列表中选择一个元素。当我尝试仅使用 Selenium C# 时,我得到了一个 Div 错误,并被建议使用 jQuery 来进行选择。Div 错误是元素应该被选择但是 div。我曾尝试使用 sendkeys 来选择下拉菜单,然后我得到它不能专注于元素的错误。

我目前有以下代码,但无法运行:

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using WebAuto;
using System.Drawing.Imaging;
using OpenQA.Selenium.Support.UI;
using Selenium.WebDriver.Extensions.JQuery;
using OpenQA.Selenium.Remote;

namespace WebAuto
{
    class AddTrade
    {
        [Test]
        public static void AddTrade1()
        {
            var driver = new ChromeDriver();
            driver.Navigate().GoToUrl("webaddress");
            var inputtext1 = driver.FindElement(OpenQA.Selenium.By.Id("lgLogin_txtUserId"));

            inputtext1.SendKeys("User");
            var inputpassword1 = driver.FindElement(OpenQA.Selenium.By.Id("lgLogin_txtPassword"));

            inputpassword1.SendKeys("Password");
            var inputbutton1 = driver.FindElement(OpenQA.Selenium.By.Id("btnLoginClient"));
            inputbutton1.Click();


            System.Threading.Thread.Sleep(3000);
            driver.FindElement(OpenQA.Selenium.By.Id("s2id_selTrader")).Click();

            $("#s2id_selTrader").val("Main Trader");

我该如何解决这个问题?

【问题讨论】:

  • 您遇到的“Div 错误”是什么?我建议您花时间弄清楚为什么您的第一种方法一开始就不起作用,因为这可能表明如果不解决问题,可能会以其他方式困扰您。您的页面上是否有多个具有相同 ID 的元素?您是否需要使用 WebDriverWait (not Thread.Sleep) 来让元素有时间加载?等
  • 您可能会重写标题以简要描述您的问题。清单 3 技术没有描述问题的含义。
  • @StriplingWarrior: 我得到错误 Element should have been select but was div

标签: c# jquery selenium


【解决方案1】:

为了使用 Selenium 运行 JavaScript,您需要使用驱动程序的 IJavaScriptExecutor。例如:

((IJavaScriptExecutor)driver).ExecuteScript("$('.toTop').remove();");

但是,我不知道这是从下拉列表中选择项目的最佳方式。您是否尝试过将元素转换为 SelectElement 并使用该类中的一种方法:

   SelectElement select = new SelectElement(element);

   select.SelectByValue("123");

还有 SelectByText 和 SelectByIndex。

【讨论】:

    【解决方案2】:

    您可以尝试添加等待子句:

    wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
    if (!driver.FindElement(By.CssSelector("selector")).Displayed)
    {
        wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("selector")));
        driver.FindElement(By.CssSelector("selector")).Text; // There are other properties and methods if you need
    }
    

    【讨论】:

    • 谢谢彼得!赞赏。
    【解决方案3】:

    如果我没记错的话,这些选择不是真正的 html select 元素(因此,您不能将 Selenium 的 SelectElement 与它们一起使用),而是一些您需要以不同方式与之交互的脚本化 div 元素。

    如果你想重现真实用户的体验,你应该尝试这样的方法

        public void SetDropdownValue(IWebElement dropdown, string value)
        {
            By valueXpath = By.XPath("//div[@id='select2-drop']//li[div[text()='" + value + "']]");
            dropdown.FindElement(By.XPath(".//b[@role='presentation']")).Click();
    
            new WebDriverWait(Driver, TimeSpan.FromSeconds(1)).Until(ExpectedConditions.ElementExists(valueXpath));
            Driver.FindElement(valueXpath).Click();
        }
    

    请注意,根据条件,该项目可能根本不可见(这将引发 ElementNotVisibleException),因此您可能需要在单击之前尝试将元素显示在视图中。如果是这种情况,您可以尝试here 描述的任何一种方法,并在检查其存在后添加它。

    另外,检查我提供的“valueXpath”模型,看看它是否与您页面中的模型相同。 li 可能在其中包含 a 元素,因此您必须直接在其中找到包含所需选项文本的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多