我收到了以下来自temp-mail.org的亚马逊电子邮件 基本上,在我使用temp-mail.org的电子邮件注册后,亚马逊已向我发送了验证码。我想使用XPath抓取/提取验证代码,并忽略电子邮件中的所有其他内容并将其另存为变量。 这是电子邮件的来源:
<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" <account-update@amazon.co.uk></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&M=urn:rtn:msg:20190112112940370f133b15924642ae9c1749c160p0eu&R=3SXE68KQSKHXF&T=O&U=https%3A%2F%2Fimages-eu.ssl-images-amazon.com%2Fimages%2FG%2F01%2Fnav%2Ftransp.gif&H=MSPPWISARSEXNQ35VMWJXUZ01L4A&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&M=urn:rtn:msg:20190112112940370f133b15924642ae9c1749c160p0eu&R=HE8PCAHPJVPN&T=E&U=https%3A%2F%2Fimages-eu.ssl-images-amazon.com%2Fimages%2FG%2F01%2Fnav%2Ftransp.gif&H=NJAGFLUUE0Y1VUAJCA4S9MABYVAA&ref_=pe_1764051_62581901_open">
</div>
</div>
</div>
</div>
如您所见,代码是
<p>451429</p>
现在,我将如何使用XPath或其他任何方法(我有一个开放的心态)来保存特定的代码(该代码在每次运行程序时都会更改)保存为名为“ verifyCode”的变量或您所希望的任何变量名称。谢谢!
使用检查元素,我发现了XPath:
//*[@cellspacing='0']/tbody/tr/td/table/tbody/tr[2]/td/p[2]
但是当我使用
var codeverify = driver.FindElement(By.XPath("//[@cellspacing='0']/tbody/tr/td/table/tbody/tr[2]/td/p[2]/text()"));
它告诉我XPath无效
答案 0 :(得分:0)
您可以使用HtmlAgilityPack金块包并使用以下代码。 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;