【问题标题】:How to click on this specific element using Selenium and C#如何使用 Selenium 和 C# 单击此特定元素
【发布时间】:2020-01-17 12:16:27
【问题描述】:

我尝试了很多方法,但似乎都没有效果,它不清楚id="ezidgrab" 可以在此网址上找到该元素:https://www.ahem.email/mailbox/QI2R89LNDT 但是如何单击带有硒的电子邮件?

我尝试过的:

1.

driver.FindElement(By.XPath("//p[contains(text(), 'Thank you for registering your email')]")).Click();

2.

driver.FindElement(By.TagName("mat-list-item")).Click();

3.

driver.FindElement(By.ClassName("mat-list-item-content")).Click();

以及链接的html:

<app-email-info _ngcontent-ahem-c5="" _nghost-ahem-c9="">
   <mat-list-item _ngcontent-ahem-c9="" 
                  class="ahem-hand-pointer mat-list-item ahem-email-unread mat-3-line">
      <div class="mat-list-item-content">
         <div class="mat-list-item-ripple mat-ripple" mat-ripple=""></div>
         <div class="mat-list-text">
            <h2 _ngcontent-ahem-c9="" class="mat-line" mat-line="">Jagex</h2>
            <p _ngcontent-ahem-c9="" 
               class="mat-line" 
               mat-line="">Thank you for registering your email</p>
            <p _ngcontent-ahem-c9="" class="mat-line" mat-line="">9 minutes ago</p>
         </div>
         <fa-icon _ngcontent-ahem-c9="" class="ng-fa-icon">
            <svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="envelope" class="svg-inline--fa fa-envelope fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
               <path fill="currentColor" d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path>
            </svg>
         </fa-icon>
      </div>
   </mat-list-item>
</app-email-info>

错误:

“OpenQA.Selenium.NoSuchElementException”类型的未处理异常发生在 WebDriver.dll 中没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//p[contains(text (), '感谢您注册电子邮件')]"}

【问题讨论】:

  • 另外请发布您遇到的错误。
  • 渲染 HTML,因为它出现在浏览器文档对象模型中吗?我们需要看到这一点。
  • 它是来自检查元素的 html。您需要从视图源中查看 html 吗?
  • 检查元素应该没问题。谢谢!
  • 您发布的 HTML 不包含电子邮件链接。你只是点击一个段落标签。由于 JavaScript,该文本实际上可以点击吗?

标签: c# selenium webdriverwait


【解决方案1】:

所需的元素是Angular 元素,因此对于Click() 元素,您必须为ElementToBeClickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mat-list-text>h2"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='mat-list-text']/h2"))).Click();
    

【讨论】:

    【解决方案2】:

    您是否尝试过点击该项目?

    driver.FindElement(By.ClassName("ahem-email-list-item")).Click();
    

    【讨论】:

    • 是的,刚刚又试了一次:OpenQA.Selenium.NoSuchElementException: '没有这样的元素:无法找到元素:{"method":"css selector","selector":".ahem\-email \-list\-item"}
    • 我在问题中发布的 HTML 中看不到这一点。该元素是否在页面的其他位置?
    • 你必须点击的项目是app-email-info标签的上一层。你在 app-email-info 范围内吗?
    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2022-11-01
    • 2021-03-12
    • 2021-12-28
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多