XPath of element preceding some text - html

Trying to find the XPath for this code:
<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1">
<span>Plan Name</span></a>
I've tried this:
//span[text()='Plan Name']
and it doesn't seem to be working.

For this HTML,
<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1"/>
<span>Plan Name</span>
This XPath,
//span[.="Plan Name"]/preceding-sibling::a[1]
will selecting the immediately preceding a elements to span elements whose string value is "Plan Name".
For this HTML,
<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1">
<span>Plan Name</span>
</a>
This XPath,
//a[.="Plan Name"]
will select all a elements with string values equal to "Plan Name".

Related

React how to concat an Anchor element (a tag) to string

I am trying to show
{`I agree to the ${(
<a href={termsFile} download>
Terms and Conditions
</a>
)}`}
but the text shows up as "I agree to the [object Object]" instead.
Simply:
<>
I agree to the{" "}
<a href={termsFile} download>
Terms and Conditions
</a>
</>
If you write it down in a single line you don't need the {" "}, you can use a normal space:
<>
I agree to the <a>Terms</a>
</>
Inside your JSX element you can try something like this
<>
I agree to the {(
<a href={termsFile} download>
Terms and Conditions
</a>
)}
</>

VBA - Click on a web page element with no id only class name

<div class="rightToolbarButtons">
<!--ko template: 'rightSearchDialogToolbarButtons' -->
<button class="main ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" data-bind="jqueryui: {widget: 'button',options: {label: DWResources.WebClient.SearchText}},command: commands.searchCommand, attr: { title: DW.Utils.format(DWResources.WebClient.SearchDialog_SearchButtonTooltip, $data.fileCabinetName()) }" role="button" aria-disabled="false" title="Search in file cabinet INVOICE (Enter)"><span class="ui-button-text">Search</span> </button>
Please see the above HTML extract i am trying to click on a "search button".
i have no difficulty with clicking on buttons with ID but i am having difficulty with tag name and unsure which tag and name i should be using.
Any help would be much appreciated.
thanks in advance
Assuming no other search buttons you can use a CSS selector of
button[title*='Search']
Which is button tag with attribute title containing string 'search'.
The "*" indicates contains and "[]" attribute.
On your HTML:
VBA:
You apply the css selector via the .selector method of document.
.document.querySelector("button[title*='Search']").Click

XPath over more levels

is it possible with xpath to extract something in another Level ?
In my table there are diffrent diffrent rows . In one if it there is my row where somewhere is print "bpmtestinstanz". I need to get the URL (href) of the the Link where is "Instanz abbauen" in the Link and in the table-row (tr) "bpmtestinstanz" inside . But I don't know how i should handle xpath over the diffrent levels of the Link and the printed text .
<tr class="htmlobject_tr even" onclick="tr_click(this, 'idp54bf6c72037f1')" onmouseover="tr_hover(this, 'idp54bf6c72037f1')" onmouseout="tr_hover(this, 'idp54bf6c72037f1')">
<td class="htmlobject_td state">
<span class="pill active">
active</span>
</td>
<td class="htmlobject_td config">
<b>
Request ID</b>
14218305642887<br>
<b>
Hostname</b>
bpmtestinstanz<br>
<b>
Typ</b>
KVM VM (localboot)<br>
<b>
CPU</b>
1<br>
<b>
Speicher</b>
1.024 GB<br>
<b>
Kernel</b>
default<br>
<b>
Festplatte</b>
5 GB<br>
<b>
Image</b>
13982361680940.cloud_14218305642887_1_<br>
<b>
IP</b>
192.168.200.166, 172.26.41.105</td>
<td class="htmlobject_td comment">
Requested by user danieldrichardt<hr>
<a class="plugin console" onclick="sshwindow = window.open('https://172.26.41.105:8022','window1722641105', 'location=0,status=0,scrollbars=yes,resizable=yes,width=973,height=500,left=100,top=100,screenX=400,screenY=100'); sshwindow.focus(); return false;" href="#">
Ajaxterm</a>
<a class="plugin novnc" target="_blank" href="api.php?action=novnc&appliance_id=14218305990082">
noVNC</a>
</td>
<td class="htmlobject_td action">
<a data-message="" class="pause" title="Instanz pausieren" href="index.php?cloud_ui=pause&cloudappliance_id[]=14218308730556">
Instanz pausieren</a>
<a data-message="" class="restart" title="Instanz neu starten" href="index.php?cloud_ui=restart&cloudappliance_id[]=14218308730556">
Instanz neu starten</a>
<a data-message="" class="private" title="Privates Image anlegen" href="index.php?cloud_ui=image_private&appliance_id=14218305990082">
Privates Image anlegen</a>
<a data-message="" class="edit" title="Instanz bearbeiten" href="index.php?cloud_ui=appliance_update&cloudappliance_id=14218308730556">
Instanz bearbeiten</a>
<a data-message="" class="remove" title="Instanz abbauen" href="index.php?cloud_ui=deprovision&cloudappliance_id[]=14218308730556">
Instanz abbauen</a>
</td>
</tr>
It can be done, but it's slightly complicated.
//tr[td[#class = 'htmlobject_td config' and contains(b[contains(., 'Hostname')]
/following-sibling::text()[1],'bpmtestinstanz')]]/td[#class = 'htmlobject_td action']
/a[contains(.,'Instanz abbauen')]/#href
which means
//tr[td[#class = 'htmlobject_td config' Select all `tr` elements anywhere in
the document, but only if they have
at least one `td` child element that
has a `class` attribute with the
value "htmlobject_td config"
and contains(b[contains(., 'Hostname')] and only if this `td` child element
also has a child element `b` whose
text value contains "Hostname"
/following-sibling::text()[1],'bpmtestinstanz')]] and if the first following sibling
of this `b` element which is a text
node contains "bpmtestinstanz"
/td[#class = 'htmlobject_td action'] of this `tr` select a child `td`
element that has a `class` attribute
with the value "htmlobject_td action"
/a[contains(.,'Instanz abbauen')]/#href and select its child `a` if its text
contains "Instanz abbauen" - and
finally select the `href` attribute
of this `a` element.
This solution requires "bpmtestinstanz" to be immediately after <b>Hostname</b>. If it can appear anywhere in the tr element, you can somewhat simplify the expression.

how to get text of few child nodes as single string

how can I get or compare text that is split into few nodes? For example:
<label>
<span class="first">first part</span>
<span class="second">second part</span>
<span class="third">third part</span>
</label>
How to get string "first partsecond partthird part" and use it for comparison with text() function?
And more complicated example:
<label>
<span class="first">first part</span>
<span class="second">second part</span>
something between
<span class="third">third part</span>
</label>
How to get string "first partsecond partsomething betweenthird part" and use it for comparison with text() function?
Obviously, wouldbe nice to strip the result from unnecessary spaces with normalize-space(), but the key is how to concatenate those children's texts.
You can use the following xpath:
//label/descendant::*/text() | //label/text()[normalize-space()]
You may see some white spaces around 'something between' which you can trim using the programming language whatever you use.

Multiline regular expression in Autoit3

I am trying to match multi-line HTML source code with a regular expression (using AutoIt). HTML source code to match:
<li class="mission">
<div>
<div class="missionTitle">
<h3>Eat a quarter-pounder with cheese</h3>
<div class="missionProgress">
<span>100%</span>
<div class="missionProgressBar" style="width: 100%;"></div>
</div>
</div>
<div class="missionDetails">
<ul class="missionRewards">
<li class="rewardCash">5,000–8,000</li>
<li class="rewardXP">XP +5</li>
</ul>
<div class="fightItems clearfix">
<h5><span>Prerequisites:</span></h5>
<div class="fightItemsWrap">
<div class="fightItem tooltip" title="Sunglasses" data-attack="Attack: 2" data-defence="Defence: 2">
<img src="/img/enhancement/3.jpg" alt="">
<span>× 1</span>
</div>
<div class="fightItem tooltip" title="Broad Shoulders" data-attack="Attack: 0" data-defence="Defence: 3">
<img src="/img/enhancement/1003.jpg" alt="">
<span>× 1</span>
</div>
<div class="fightItem tooltip" title="Irish Fond Anglia" data-attack="Attack: 4" data-defence="Defence: 8">
<img src="/img/enhancement/2004.jpg" alt="">
<span>× 1</span>
</div>
</div>
</div>
<form action="/quest/index/i/kdKJBrgjdGWKqtfDrHEkRM2duXVn1ntH/h/c0b2d58642cd862bfad47abf7110042e/t/1336917311" method="post">
<input type="hidden" id="id" name="id" value="17"/>
<button class="button buttonIcon btnEnergy"><em>5</em></button>
</form>
</div>
</div>
</li>
It is present multiple times on a single page (but items within <div class="fightItems clearfix">...</div> vary).
I need to match
<h3>Eat a quarter-pounder with cheese</h3>,
the first span <span>100%</span> and
<input type="hidden" id="id" name="id" value="17"/>.
Expected result (for every occurrence on a page):
$a[0] = "Eat a quarter-pounder with cheese"
$a[1] = "100%"
$a[2] = "17"
What I came up with:
(?U)(?:<div class="missionTitle">\s+<h3>(.*)</h3>\s+<div class="missionProgress">\s+<span>(.*)</span>)|(?:<form .*\s+.*<input\stype="hidden"\sid="id"\sname="id"\svalue="(\d+)"/>\s+.*\s+</form>)
But that leaves some array-items empty. I also tried the (?s) flag, but then it only captures first occurrence (and stops matching after).
I had not to use . to match words or integers, because of the (?s) flag. The correct regex is:
(?U)(?s)<div class="missionTitle">\s+<h3>([\w\s]+)</h3>(?:.*)<div class="missionProgress">\s+<span>(\d+)%</span>(?:.*)<input.* value="(\d+)"/>
Regular expression to match multi-line HTML source code:
As per documentation;
\R matches newline characters (?>\r\n|\n|\r),
dot . does not (unless (?s) is set).
\s matches white space characters.
Generally some combination is required (like \R\s*?).
Non-capturing groups are redundant (match without capturing instead).
If uniquely enclosed, single characters may be excluded instead (like attribute="([^"]*?)" for text between double-quotes).
Example (contains double-quotes; treat as per Documentation - FAQ - double quotes):
(?s)<div class="missionTitle">.*?<h3>(.*?)</h3>.*?<div class="missionProgress">.*?<span>([^<]*?)</span>.*?<input type="hidden" id="id" name="id" value="([^"]*?)"/>
Visual explanation:
If regular expressions should be used on HTML (beyond simple listings like this) is a different question (been, done, T-shirt).