【问题标题】:Xpath or Css selector for this element - HELPP此元素的 Xpath 或 Css 选择器 - HELPP
【发布时间】:2021-12-22 00:48:25
【问题描述】:

对于我的 Selenium c# 测试,我花了大约 5 个小时试图找到该成功消息的选择器,但运气不佳。我的测试总是失败,说找不到元素!!

知道我可以使用什么选择器吗???

我已经尝试过所有插件的 xpath、Css、Classname...

public IWebElement alertSuccessfulAlert => WebDriver.FindElement(By.XPath("//div[contains(text(),'Registration is successful')]"));

public IWebElement alertSuccessfulAlert => WebDriver.FindElement(By.CssSelector("body:nth-child(2) my-app:nth-child(1) div.container:nth-child(2) > main.row:nth-child(1)"));

【问题讨论】:

  • 您的 XPath 看起来正确。你看到什么错误?也许您只是在访问该元素之前错过了一些延迟?
  • 使用这个通用的 xpath: Message = driver.find_element(By.XPATH, '//button[contains(.,"Register")]//following::div[contains(@class,"警报")]')
  • 可以分享网页网址吗?
  • 我得到的错误是找不到元素,它没有通过 Xpath、CSS 等找到选择器

标签: c# selenium css-selectors


【解决方案1】:

气泡消息注册成功是通过JavaScript生成的,因此您必须将WebDriverWait诱导为所需的ElementToBeClickable(),您可以使用以下Locator Strategies之一:

  • XPath:

      public IWebElement alertSuccessfulAlert = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(., 'Registration is successful')]")));
    

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2020-04-23
    相关资源
    最近更新 更多