C# 例外情况';元素本应为select,但为div';碳硒

C# 例外情况';元素本应为select,但为div';碳硒,c#,selenium-webdriver,C#,Selenium Webdriver,我得到了一条代码异常。我的代码是将物品添加到袋子中,然后我要选择物品的数量。当我点击下拉菜单上的数量选择2时 我的代码在此行引发异常: IWebElement Qty = webDriver.FindElement(By.Id("bagApp")); SelectElementFromDropDown(Qty, "2"); 元素本应为select,但为div 这段代码旨在单击下拉菜单 using System; using System.Collections.Generic; using S

我得到了一条代码异常。我的代码是将物品添加到袋子中,然后我要选择物品的数量。当我点击下拉菜单上的数量选择2时

我的代码在此行引发异常:

IWebElement Qty = webDriver.FindElement(By.Id("bagApp"));
SelectElementFromDropDown(Qty, "2");
元素本应为select,但为div

这段代码旨在单击下拉菜单

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class Exercise3
    {

        static void Main()
        {
            IWebDriver webDriver = new ChromeDriver();


            webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
            webDriver.Manage().Window.Maximize();

            webDriver.FindElement(By.XPath(".//input[@data-testid='search-input']")).SendKeys("nike trainers");

            webDriver.FindElement(By.XPath(".//button[@data-testid='search-button-inline']")).Click();

            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
            IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));


            webDriver.FindElement(By.CssSelector("article img")).Click();


            IWebElement Size = webDriver.FindElement(By.XPath(".//select[@data-id='sizeSelect']"));
            SelectElementFromDropDown(Size, "UK 10.5 - EU 45.5 - US 11.5");


            webDriver.FindElement(By.XPath("//*[@data-bind='text: buttonText']")).Click();

            webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();

            IWebElement Qty = webDriver.FindElement(By.Id("bagApp"));
            SelectElementFromDropDown(Qty, "2");

            webDriver.FindElement(By.XPath("//*[@data-bind='click: update']")).Click();

            //int trainer = 145;

            //while (trainer < 200){
            //    Console.WriteLine(trainer);
            //    trainer = trainer * 2;


            //}

            webDriver.Quit();
        }

        private static void SelectElementFromDropDown(IWebElement ele, string text)
        {
            SelectElement select = new SelectElement(ele);
            select.SelectByText(text);
        }
    }
}

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用OpenQA.Selenium;
使用OpenQA.Selenium.Chrome;
使用OpenQA.Selenium.Support.UI;
使用OpenQA.Selenium.Interactions;
使用系统线程;
命名空间练习1
{
课堂练习3
{
静态void Main()
{
IWebDriver webDriver=新的ChromeDriver();
webDriver.Navigate().GoToUrl(“http://www.asos.com/men/");
webDriver.Manage().Window.Maximize();
webDriver.FindElement(By.XPath(“.//input[@data testid='search-input'])).SendKeys(“nike训练器”);
webDriver.FindElement(按.XPath(“.//button[@data testid='search-button-inline'])。单击();
WebDriverWait wait=新的WebDriverWait(webDriver,TimeSpan.FromSeconds(5));
IWebElement country=wait.Until(ExpectedConditions.Element存在(由.CssSelector(“article img”));
webDriver.FindElement(By.CssSelector(“article img”))。单击();
IWebElement Size=webDriver.FindElement(By.XPath(“.//select[@data id='sizeSelect']);
从下拉列表中选择元素(大小为“英国10.5-欧盟45.5-美国11.5”);
webDriver.FindElement(By.XPath(“//*[@data bind='text:buttonext'])。单击();
webDriver.FindElement(By.XPath(“//a[@data testid='bagIcon']))。单击();
IWebElement数量=webDriver.FindElement(By.Id(“bagApp”);
从下拉列表中选择元素(数量,“2”);
webDriver.FindElement(By.XPath(“/*[@data bind='click:update'])。单击();
//int trainer=145;
//同时(培训师<200){
//控制台。WriteLine(培训师);
//培训师=培训师*2;
//}
webDriver.Quit();
}
私有静态void selectElement from下拉列表(IWebElement元素,字符串文本)
{
SelectElement select=新的SelectElement(ele);
选择。按文本选择(文本);
}
}
}

您的下拉列表由div和span组成,在这种情况下,选择类对您没有帮助

您可以尝试以下代码:

IList<IWebElement> options= webDriver.FindElements(By.CssSelector("li[class*='select2-results__option']"));  
foreach (IWebElement element in options){  
     if(element.GetText().Equals("2")){  

        element.Click();
    }
    }

在移动到此操作的新页面时,应使用显式等待。

您的下拉列表由div和span组成,在这种情况下,选择类对您没有帮助

您可以尝试以下代码:

IList<IWebElement> options= webDriver.FindElements(By.CssSelector("li[class*='select2-results__option']"));  
foreach (IWebElement element in options){  
     if(element.GetText().Equals("2")){  

        element.Click();
    }
    }

在移动到此操作的新页面时,应使用显式等待。

您可以如下选择webelement

//Explicit wait is added to ensure that my bag item section is loaded successfully
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));

IWebElement qtyElement = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
SelectElementFromDropDown(qtyElement,"2");

您可以如下选择webelement

//Explicit wait is added to ensure that my bag item section is loaded successfully
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));

IWebElement qtyElement = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
SelectElementFromDropDown(qtyElement,"2");

发生此错误的原因是,您在HTML元素上使用的是
SelectElement
类,在本例中,该HTML元素不是
SELECT
,而是
DIV

要选择所需选项,您需要单击下拉列表打开它,然后从下拉列表中单击所需选项。由于您可能会不止一次地选择选项,因此最好将用于选择选项的代码放入函数中

public void SelectOption(string s)
{
    new WebDriverWait(webDriver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector($"span[title='{s}']"))).Click();
}
那就这样说吧

webDriver.FindElement(By.CssSelector("span.select2")).Click();
SelectOption("2");

发生此错误的原因是,您在HTML元素上使用的是
SelectElement
类,在本例中,该HTML元素不是
SELECT
,而是
DIV

要选择所需选项,您需要单击下拉列表打开它,然后从下拉列表中单击所需选项。由于您可能会不止一次地选择选项,因此最好将用于选择选项的代码放入函数中

public void SelectOption(string s)
{
    new WebDriverWait(webDriver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector($"span[title='{s}']"))).Click();
}
那就这样说吧

webDriver.FindElement(By.CssSelector("span.select2")).Click();
SelectOption("2");


2这个问题是这行代码的延续吗
SelectElement select=new SelectElement(ele)
ele应该是Select类型的Web元素。Id(“bagApp”)给出了一个div类型的Web元素。因此有例外。在将元素分配给数量之前,请尝试获取具有Select标记的元素的定位器。请花一分钟将HTML从您的注释移动到您的问题中,并正确设置其格式。同时减少您发布到的代码。2这个问题是这行代码的延续吗
SelectElement select=new SelectElement(ele)
ele应该是Select类型的Web元素。Id(“bagApp”)给出了一个div类型的Web元素。因此有例外。在将元素分配给数量之前,请尝试获取具有Select标记的元素的定位器。请花一分钟将HTML从您的注释移动到您的问题中,并正确设置其格式。同时减少您发布到的代码。这会阻止异常发生吗?请注意,您必须单击向下箭头按钮/span,让您的webdriver知道中存在元素。因此,您可以从我更新的答案中获取代码。用观察结果更新我。这会阻止异常发生吗?请注意,您必须单击向下箭头按钮/span,让您的webdriver知道中存在元素。因此,您可以从我更新的答案中获取代码。更新我的观察结果。我现在没有得到这样的元素异常。不要认为行李物品数量是有效的,您可以试试这个
IWebElement quantityDropDown=webDriver.FindElement(By.XPath(//select[@class='bag-item-quantity行李物品选择器select2 hidden accessible'])仍然无法找到它。您可以通过进入>搜索Nike训练器>单击第一个>选择任意尺寸并添加到包>进入包的右上角>将数量更改为2来查看此信息。我可以使用以上两个选项突出显示上述站点中的“数量”下拉列表xpath@J.dockster:请在上述操作之前添加一些等待时间