How to click on this specific element using Selenium and C#

寵の児 提交于 2021-02-15 06:22:51

问题


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?

What I have tried:

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();

and the html of the link:

<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>

Error:

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')]"}


回答1:


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();
    



回答2:


Have you tried to click on the item?

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


来源:https://stackoverflow.com/questions/59787187/how-to-click-on-this-specific-element-using-selenium-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!