c# - 即使没有重复的类名,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; }
}
}
解决方案
使用 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();;
推荐阅读
- python-3.x - 如何在 pygame 中读取、写入和更新您的游戏高分 from/to file.txt saved.?
- python - Python类方法继承
- java - 如何修复 int Bitmap getWidth() 的空引用错误
- html - 是否可以使用 :hover 更改图像?
- ruby-on-rails - 如何在 embeds_many mongoid rails 应用程序中按嵌入式模型字段排序?
- reactjs - 使用 BrowserRouter 响应 js:当我部署到 apache 服务器时它是空白的
- python - Altair 等值线图,基于折线图选择的颜色高亮
- ios - m4a 文件使用 avplayer 播放,但不使用 avaudioplayer
- django - Django一父二子关系
- python - 如何检查 Python 的用户定义函数天气传递给它的列表 [] 只包含整数?