C#硒异常“元素应已选择但为div" [英] Exception 'Element should have been select but was div' C# selenium

查看:74
本文介绍了C#硒异常“元素应已选择但为div"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一段代码异常.我的代码是将商品添加到袋子中,然后我要选择商品的数量.当我单击下拉菜单上的数量以选择2时.

I'm getting an exception with a piece of code. My code is adding an item to a bag then I want to select the quantity of the item. When I click on the quantity on the drop down menu to select 2.

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

My code throws an exception on this line:

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

元素应已选择,但已被分割.

Element should have been select but was div.

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

This piece of code is designed to click on the drop down menu.

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

推荐答案

您的下拉菜单由 div spans 组成,在这种情况下,Select类将无济于事.

You drop down is made of Divs and spans, Select class will not help you in this case.

您可以尝试以下代码:

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

        element.Click();
    }
    }

请注意,在尝试从下拉列表中选择值之前,您必须先单击向下箭头按钮,然后才能使用以下代码:

Note that before trying to select value from drop down , you have to click on down arrow button for that you can use this code :

webDriver.FindElement(By.CssSelector("span#select2-d2bx-container+span")).Click() 

在移至新页面进行此操作时,应使用显式等待.

You should use explicit wait as you are moving to new page for this operation.

这篇关于C#硒异常“元素应已选择但为div"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆