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">
<span class="glyphicon glyphicon-chevron-left"></span> Back to list
</li>
</ul>
<ul class="reset rcol">
<li class="lcol">
<span class="glyphicon glyphicon-download-alt"></span> Download
</li>
<li class="lcol">
<span class="glyphicon glyphicon-remove"></span> Delete
</li>
<li class="lcol">
<span class="glyphicon glyphicon-wrench"></span> Source
</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>
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;
Related
I want to display an anchor tag inside angular expression enclosed by html tag. when I am using it inside html tag, it is displaying the raw path as it is which is:
<a href='../UserControls/DownloadRLCSFile.ashx?Path=\\\\dotnetdev\\csv\\RLCSDocuments\\Registrations\\ABACF\\E.S.I.C. Registration\\Sample.xlsx' target='_blank' download><i class='fa fa-download' style='font-size: 13pt;' ></i></a> Code :
<div id="Div2" ng-controller="BasicRegulatoryDetail">
<!--added by dilip -->
<h4 class="padder-sm b-b"><b>Regulatory Updates</b></h4>
<div>
<div class="table-responsive">
<table class="table table-striped bg-white ">
<%--<thead>
<tr>
<th>Subject</th>
<th>Download</th>
</tr>
</thead>--%>
<tbody>
<tr ng-repeat="bs in BasicInfo">
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="~/RLCS_Connect/RegulatoryUpdateDetails.aspx?Subject={{bs.Subject}}" runat="server">{{bs.Subject}}</asp:HyperLink>
</td>
<td>{{bs.Document_Path}}</td>//here i am facing the problem </tr>
</tbody>
</table>
</div>
</div>
</div>
use ng-bind-html something like
<td><span ng-bind-html='bs.Document_Path'></span></td>
I have a form editor web application for creating forms and workflows. The home page of the application contains a menu on the left side.
The menu looks like .
when I mouseover any tab, I get a tool tip.
I am currently trying to verify this using selenium webdriver and c#. Following is the HTML for the menu
<div class="tabmenu">
<div class="tab-list">
<div class="mask"></div>
<ul class="level1" style="left: 0px;">
<li class="l1-item top" title="">
<span class="selection-top"></span>
<table class="mover">
<tbody>
<tr>
<td class="tooltip">
<span>Create Flow</span>
</td>
<td>
<span class="logo logo12">
<span class="status status-none"></span>
</span>
</td>
</tr>
</tbody>
</table>
<span class="selection-bottom"></span>
<div class="hr"></div>
</li>
<li class="l1-item prev" title="">
<span class="selection-top"></span>
<table class="mover" style="left: -110px;">
<tbody>
<tr>
<td class="tooltip">
<span>Dashboard</span>
</td>
<td>
<span class="logo logo11">
<span class="status status-none"></span>
</span>
</td>
</tr>
</tbody>
</table>
<span class="selection-bottom"></span>
<div class="hr"></div>
</li>
</ul>
</div>
</div>
When I mouseover any tab, the <li class="l1-item> have another class added <li class="l1-item tooltip">
Following is the code that I have written
[FindsBy(How = How.CssSelector, Using = "ul.level1 > li")]
[CacheLookup]
private IList<IWebElement> LeftMenuItems;
public bool isToolTipPresentOnEachTab()
{
foreach (IWebElement ele in LeftMenuItems)
{
Actions action = new Actions(driver);
action.MoveToElement(ele).Perform();
if((ele.FindElement(By.CssSelector("td.tooltip > span")).GetAttribute("textContent")) != "Create Flow"
&& !ele.GetAttribute("class").Contains("showtooltip")){
return false;
}
}
return true;
}
I am getting the tooltip only in the first iteration of the loop and the test fails in the 2nd iteration.
Can someone help me with this ?
I am trying to located input element "mpcname" in js file which is under div and table elements.
The code I have written to locate the element is as follows but is not detecting the Input element with the name mpcName in it.
Driver.FindElement(By.XPath(".//input[contains(.,'mpcName')]"));
The HTML code looks like below.
<div enable-tooltips="" class="ng-scope">
<form name="InstanceDetailForm" ng-submit="doApply()" novalidate="" class="ng-pristine ng-invalid ng-invalid-required ng-valid-unnamed ng-valid-maxlength ng-valid-not-integer">
<table class="table-condensed" style="width: 100%;">
<tbody><tr>
<td></td>
</tr>
<tr>
<td>
<table style="width: 100%;">
<tbody><tr>
<td class="pull-left">
<h4 class="ng-binding">Controller Configuration<b ng-show="InstanceIsDirty()" class="ng-hide"> *</b></h4>
</td>
<td class="pull-right">
<button id="btnApply" type="submit" class="btn btn-primary" data-toggle="tooltip" data-placement="bottom" title="Save an APC instance">
<span class="glyphicon glyphicon-save"></span> Apply
</button>
<button id="btnExportConfig" type="button" class="btn btn-primary" ng-click="goToInstanceList()" data-toggle="tooltip" data-placement="bottom" title="Cancel save and redirect to APC instance list page.">
<span class="glyphicon glyphicon-remove"></span> Cancel
</button>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td>
<table style="width: 100%" class="table-condensed table-striped table-bordered">
<tbody><tr>
<td>Name</td>
<td>
**<input id="mpcname" name="mpcname" ng-model="mpcname" value="" ng-required="true" is-named="" size="50" autocomplete="off" ng-disabled="!allowEditInstanceName(mpcname,state)" class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-unnamed ng-touched" required="required" type="text">**
With xpath ".//input[contains(.,'mpcName')]" you are actually looking for an <input> element with text mpcName. mpcName is actually the id (and name and ng-model) attribute. You can use
Driver.FindElement(By.Id("mpcName"));
Or
Driver.FindElement(By.Name("mpcName"));
following code may be helpful
Driver.FindElement(By.XPath(".//input[contains(#id,'mpc')]");
Driver.FindElement(By.XPath(".//input[contains(#class,'ng-pris')]");
I want to develop a program to automatically lookup words in Longman online dictionary and copy its definition and meanings. I am using visual studio and C# language and I have developed the part which browse to the website and search for a word. However, the problem is in navigating through Longman online website when there are some word forms. for example for this link the html code of the suggested words is as following:
<div class="content1">
<style>
.dictionary-results-title .topic_bullet {
margin: 0px;
}
</style>
<div class="border-search">
<div class="dictionary-results-title">
Results from the Longman Dictionary of Contemporary English:
</div>
<div class="dictionary-results-title">
<span class="dictionary-results-title-topic-new">
Click on topic labels to navigate through our Topic Dictionary
</span>
</div>
<!-- google_ad_section_start -->
<div id="42385" class="folded">
<table id="hwdfolded" class="hwdfolded" cellspacing="0" cellpadding="0">
<tr>
<td class="hwdunSelHG"></td>
<td class="hwdunSelHM"></td>
<td class="hwdunSelHD"></td>
</tr>
<tr>
<td class="hwdunSelMG"></td>
<td class="hwdunSelMM">
<a href="/dictionary/superman">
<span class="headword">superman</span></a>
<span class="homographs"></span>
<span class="wordclass">noun</span>
<span class="topiclinks"></span>
</td>
<td class="hwdunSelMD"></td>
</tr>
<tr>
<td class="hwdunSelBG"></td>
<td class="hwdunSelBM"></td>
<td class="hwdunSelBD"></td>
</tr>
</table>
</div>
<div id="42386" class="folded">
<table id="hwdfolded" class="hwdfolded" cellspacing="0" cellpadding="0">
<tr>
<td class="hwdunSelHG"></td>
<td class="hwdunSelHM"></td>
<td class="hwdunSelHD"></td>
</tr>
<tr>
<td class="hwdunSelMG"></td>
<td class="hwdunSelMM">
<a href="/dictionary/Superman">
<span class="headword">Superman</span></a>
<span class="homographs"></span>
<span class="wordclass"></span>
<span class="topiclinks"></span>
</td>
<td class="hwdunSelMD"></td>
</tr>
<tr>
<td class="hwdunSelBG"></td>
<td class="hwdunSelBM"></td>
<td class="hwdunSelBD"></td>
</tr>
</table>
</div>
<script language="JavaScript" type="text/javascript">
parent.curEntryId=42385; parent.prevEntryId=42385; parent.nextEntryId=42385;
parent.gsSenseId=null; parent.giPhrId=null;
</script>
</div>
</div>
I have found the way to find the ID of the words like id="42385" and id="42386" but I cannot navigate through them. There is a table inside each element with these ids. As you can see in the html code the second data of the second row of the table contains the links for each word.
the code I have written to click on them is like this:
HtmlElement Word = webBrowser1.Document.GetElementById("hwdfolded");
foreach (HtmlElement ele in Word.Parent.Parent.Children)
{
if (ele.Id != null && ele.InnerText.ToLower().Contains(Stword))
{
HtmlElement clickon = webBrowser1.Document.GetElementById(ele.Id);
clickon.InvokeMember("click");
//ele.InvokeMember("click");
while (webBrowser1.ReadyState != WebBrowserReadyState.Interactive)
Application.DoEvents();
do
{
Application.DoEvents();
} while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
break;
}
}
Note that Stword contains the string of the word I am searching for in this example it contains "superman" and also the ele.Id contains one the specified Ids and I checked it in debug mode. But the click command not works. I will appreciate it if you can tell me the solution or give me another solution which is better.
I suggest that you use a scraping tool to perform the navigation through the page. With Selenium it is really easy to obtain elements by XPATH and navigate through them and also obtain the text inside them. Hope it helps.
I have a scenario where there are three distinct html parent elements
<tr class="s3288" data-id="s3288">
<tr class="s3288" data-id="s3288">
<tr class="s1" data-id="s1">
Two these elements have a child element
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
but third one does not have this child element.
in my test scenario, I have to click this child element if present.
Is there any function by which i can detect if this element is present?
I have used following function bug for some reason it does not work properly
function i used
protected bool IsElementPresent(By by)
{
try
{
wait.Until(drv => (drv.FindElement(by)));
return true;
}
catch
{
return false;
}
}
Here is how the html of browser looks like
<tr class="s1" data-id="s1">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_s1_0_true" value="">
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>
<tr class="s3288" data-id="s3288">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_s3288_0_true" value="">
<a class="gylink mapvl" title="No Map Values" href="#">Map Values</a>
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>
<tr class="t1" data-id="t1">
<td class="ftc1">
<td class="ftc2">
<td class="ftc3 valuecol">
<td class="ftc4 headcol">
<ul class="multihead">
<li>
<input class="txtinput chghead headcolfield pholder changit requiref" type="text" placeholder="Enter Column Header Value" validate="{required: true, messages: {required: 'Required'}}" name="th_t1_0_true" value="">
</li>
</ul>
</td>
<td class="ftc5 deletecol"> </td>
</tr>