简体   繁体   中英

I need help to implement XPath

I have the following email from amazon that is from temp-mail.org Basically amazon has sent me a verification code after i signed up with an email from temp-mail.org. I would like to use XPath to scrape/extract that Verification code and ignore everything else in the email and save it as a variable. Here is the source for the email:

<div class="col-md-7 col-md-offset-0 col-sm-10 col-sm-offset-2 col-xs-12 ord2">
  <div class="content main" style="opacity: 100;">
    <div class="mailView" style="display: block">
      <div class="pm-ctrl clearfix">
        <ul class="reset lcol">
          <li class="lcol">
            <a href="https://temp-mail.org/en/" class="linkbord"> <span class="glyphicon glyphicon-chevron-left"></span> Back to list</a>
          </li>
        </ul>
        <ul class="reset rcol">
          <li class="lcol">
            <a href="https://temp-mail.org/en/download/b0eca4518ecb2fcc8dbca54152936099" class="linkbord no-ajaxy"> <span class="glyphicon glyphicon-download-alt"></span> Download</a>
          </li>
          <li class="lcol">
            <a href="https://temp-mail.org/en/delete/b0eca4518ecb2fcc8dbca54152936099" class="linkbord click-to-delete-mail"> <span class="glyphicon glyphicon-remove"></span> Delete</a>
          </li>
          <li class="lcol">
            <a href="https://temp-mail.org/en/source/b0eca4518ecb2fcc8dbca54152936099" target="_blank" class="linkbord no-ajaxy"> <span class="glyphicon glyphicon-wrench"></span> Source</a>
          </li>
        </ul>
      </div>
      <div class="pm-info">
        <h4 class="pm-subject">Amazon password assistance</h4>
        <ul class="reset">
          <li><span class="glyphicon glyphicon-user grey"></span> From: "Amazon.co.uk" &lt;account-update@amazon.co.uk&gt;</li>
          <li><span class="glyphicon glyphicon-time grey"></span> Date: 12-01-2019 11:19:27</li>
        </ul>
      </div>
      <div class="pm-text">
        <div data-x-div-type="html" xmlns="http://www.w3.org/1999/xhtml">
          <div data-x-div-type="body">


            <img width="1" height="1" src="https://www.amazon.co.uk/gp/r.html?C=4XBRCDY40J48&amp;M=urn:rtn:msg:20190112112940370f133b15924642ae9c1749c160p0eu&amp;R=3SXE68KQSKHXF&amp;T=O&amp;U=https%3A%2F%2Fimages-eu.ssl-images-amazon.com%2Fimages%2FG%2F01%2Fnav%2Ftransp.gif&amp;H=MSPPWISARSEXNQ35VMWJXUZ01L4A&amp;ref_=pe_1764051_62581901_opens">
            <table
              align="center" cellspacing="0" cellpadding="0">
              <tbody>
                <tr>
                  <td>
                    <table cellspacing="0" cellpadding="0">
                      <tbody>
                        <tr>
                          <td>
                            <table cellspacing="0" cellpadding="0">
                              <tbody>
                                <tr>
                                  <td width="250">
                                    <img src="https://images-na.ssl-images-amazon.com/images/G/01/x-locale/cs/te/logo._CB152417367_.png"></td>
                                  <td width="250" valign="top" align="right">
                                    <p>Password assistance</p>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <p>To verify your identity, please use the following code:</p>
                            <p>451429</p>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <p>Amazon takes your account security very seriously. Amazon will never email you and ask you to disclose or verify your Amazon password, credit card or banking account number. If you receive a suspicious email with a link to
                              update your account information, do not click on the link—instead, report the email to Amazon for investigation.

                            </p>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <p>We hope to see you again soon.
                            </p>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </td>
                </tr>
              </tbody>
              </table><img width="1" height="1" src="https://www.amazon.co.uk/gp/r.html?C=4XBRCDY40J48&amp;M=urn:rtn:msg:20190112112940370f133b15924642ae9c1749c160p0eu&amp;R=HE8PCAHPJVPN&amp;T=E&amp;U=https%3A%2F%2Fimages-eu.ssl-images-amazon.com%2Fimages%2FG%2F01%2Fnav%2Ftransp.gif&amp;H=NJAGFLUUE0Y1VUAJCA4S9MABYVAA&amp;ref_=pe_1764051_62581901_open">
          </div>
        </div>
      </div>
    </div>

As you can see, the code is

<p>451429</p>

Now how would i go about using XPath, or anything else (I have an open mind), to save that specific Code which will change everytime i run the program, to a variable called "verifyCode" or whatever variable name you please. Thank you!

Using inspect element, i have found the XPath which is:

//*[@cellspacing='0']/tbody/tr/td/table/tbody/tr[2]/td/p[2]

but when i use

var codeverify = driver.FindElement(By.XPath("//[@cellspacing='0']/tbody/tr/td/table/tbody/tr[2]/td/p[2]/text()"));

it tells me XPath is invalid

You can use HtmlAgilityPack nugget package and use below code. Input.html is the HTML which you mentioned in your question.

var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(File.ReadAllText("Input.html"));

            var element = htmlDoc.DocumentNode.Descendants()
                .Where(x => x.InnerText == "To verify your identity, please use the following code:")
                .ToList()
                .FirstOrDefault();
            while (element.NextSibling.Name != "p")
                element = element.NextSibling;

            var code= element.NextSibling.InnerText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM