如何使用Selenium和C#单击此特定元素 [英] How to click on this specific element using Selenium and C#

查看:52
本文介绍了如何使用Selenium和C#单击此特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多方法,但是似乎都无法正常工作 id ="ezidgrab" 该元素可以在以下网址中找到:

I've tried lots of ways but none seem to work its not a clear id="ezidgrab" the element can be found on this url: https://www.ahem.email/mailbox/QI2R89LNDT but how do I click the email there with selenium?

我尝试过的事情:

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>

错误:

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

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll no such element: Unable to locate element: {"method":"xpath","selector":"//p[contains(text(), 'Thank you for registering your email')]"}

推荐答案

所需元素是 Angular 元素,因此在必须诱使 ElementToBeClickable() WebDriverWait 的元素上 Click(),您可以使用以下定位器策略:

The desired element is an Angular element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following 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();
    

  • 这篇关于如何使用Selenium和C#单击此特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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