首页 > 解决方案 > 即使没有重复的类名,Selenium 也无法定位元素

问题描述

即使没有重复的类名,我的代码也无法在 Nordstrom 机架网站上找到元素。元素帐户(注册后)未得到识别。

using NordstromRack.UI_Elements;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;

namespace NordstromRack
{
    class EntryPoint
    {
        static void Main(string[] args)
        {
            String parentHandle = Driver.driver.CurrentWindowHandle; // get the current window handle
            EmailSignUp signup = new EmailSignUp();
            ProfileUpdate profile = new ProfileUpdate();
            Driver.driver.Navigate().GoToUrl("https://www.nordstromrack.com/");
            Driver.driver.Manage().Window.Maximize();
            Thread.Sleep(1000);
            signup.SignUpLink.Click();

            foreach (String winHandle in Driver.driver.WindowHandles)
            {
                Driver.driver.SwitchTo().Window(winHandle);
            }
            //WebDriverWait wait = new WebDriverWait(Driver.driver, TimeSpan.FromSeconds(1000));
            //wait.Until(ExpectedConditions.ElementToBeClickable(signup.EmailInput));
            signup.EmailInput.Click();
            signup.EmailInput.SendKeys(Config.Credentials.Valid.BaseEmail);
            Thread.Sleep(1000);
            signup.Password.Click();
            signup.Password.SendKeys(Config.Credentials.Valid.Password);
            Thread.Sleep(1000);
            signup.Password.Submit();
            //signup.SignOut.Click();
            //WebDriverWait wait = new WebDriverWait(Driver.driver, TimeSpan.FromSeconds(1000));
            //wait.Until(ExpectedConditions.ElementToBeClickable(profile.Account));
            Thread.Sleep(2000);
            Driver.driver.SwitchTo().Window(parentHandle);
            Thread.Sleep(2000);
            profile.Account.Click();**//Unable to locate this element**
            Thread.Sleep(5000);
            //Driver.driver.Quit();

        }
    }
}

Profile.cs 类详细信息,其中包含帐户元素的定位器详细信息。我究竟做错了什么? 抛出的错误:找不到元素:By.CssSelector:secondary-nav__link.secondary-nav__link--account

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

namespace NordstromRack.UI_Elements
{
    public class ProfileUpdate
    {
        public ProfileUpdate()
        {
            PageFactory.InitElements(Driver.driver, this);
        }

        [FindsBy(How = How.CssSelector, Using = "input.form-label__input.form-label__input--password")]
        public IWebElement Profile { get; set; }

        [FindsBy(How = How.CssSelector, Using = "secondary-nav__link.secondary-nav__link--account")]
        public IWebElement Account { get; set; }
    }
}

标签: c#seleniumselenium-webdriverselenium-chromedriver

解决方案


使用 Css Sector 时,不必给出整个类名。您可以给出tagname.any of the class attribute.any of the class attribute 取而代之

"secondary-nav__link.secondary-nav__link--account"

你可以试试css选择器吗

 WebElement element1=driver.findElement(By.cssSelector("a.secondary-nav__link"));
        Actions action = new Actions(driver);
        action.moveToElement(element1).click().build().perform();;

推荐阅读