如何使用Selenium和C#单击此特定元素

问题描述 投票:2回答:2

我已经尝试了很多方法,但是似乎都无法正常工作id="ezidgrab",但可以在以下URL上找到该元素: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>

错误:

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')]"}
c# selenium xpath css-selectors webdriverwait
2个回答
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();
    

0
投票

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

driver.FindElement(By.ClassName("ahem-email-list-item")).Click();
© www.soinside.com 2019 - 2024. All rights reserved.