简体   繁体   English

我需要帮助来实现XPath

[英]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. 我收到了以下来自temp-mail.org的来自亚马逊的电子邮件,基本上,在我使用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. 我想使用XPath抓取/提取验证代码,并忽略电子邮件中的所有其他内容并将其另存为变量。 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. 现在,我将如何使用XPath或其他任何方法(我有一个开放的心态)将特定的代码保存到一个名为“ verifyCode”的变量或您喜欢的任何变量中,该特定的Code每次我运行该程序都会更改。 Thank you! 谢谢!

Using inspect element, i have found the XPath which is: 使用检查元素,我发现了XPath是:

//*[@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 告诉我XPath无效

You can use HtmlAgilityPack nugget package and use below code. 您可以使用HtmlAgilityPack块包并使用以下代码。 Input.html is the HTML which you mentioned in your question. Input.html是您在问题中提到的HTML。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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