Get specific text value of parent without child - xpath

I'm trying to use Xpath to get the text of the parent anchor without also getting the text from the span of the example below:
<a id="readingListBtn" class="btn btn-transparent" title="Reading List" href="javascript:void(0);">
<span class="icon icon_headerBookmark">Header Bookmark</span>
0
</a>
The Xpath I'm using (//a[#id = 'readingListBtn']) returns "Header Bookmark0", but I'm just interested in the "0" part.

Just get the direct text child:
//a[#id = 'readingListBtn']/text()

Related

Proper xpath Syntax for Extracting Two Text Values

I am trying to scrape a web page for NAME OF COMPANY and CITY AND STATE OF COMPANY shown below.
I have an xpath code snippet that identifies both text elements at the same time:
// span[starts-with(#class,"text-align")]/text()[2]
This xpath snippet pulls the first text value (COMPANY NAME). How do I get the second text element (CITY,STATE)?
A snip of the web page code looks like this:
<div>
<ul class="pv-top-card-v3--experience-list">
<li>
<a class="pv-top-card-v3--experience-list-item" href="#" data-control-name="position_see_more" data-ember-action="" data-ember-action-172="172">
<img src="https://media.licdn.com/dms/image/C4E0BAQFhA8h46hvabA/company-logo_100_100/0?e=1582761600&v=beta&t=VAeZqaGu3Lu6Ol_n5kiiI74FSRuSOZA1ggAI5qTVRjE" id="ember173" class="EntityPhoto-square-1 flex-shrink-zero ember-view">
<span id="ember174" class="text-align-left ml2 t-14 t-black t-bold full-width lt-line-clamp lt-line-clamp--multi-line ember-view" style="-webkit-line-clamp: 2"> THIS IS THE NAME OF A COMPANY
<!----></span>
</a>
</li>
<li>
<a class="pv-top-card-v3--experience-list-item" href="#" data-control-name="education_see_more" data-ember-action="" data-ember-action-176="176">
<img src="https://media.licdn.com/dms/image/C560BAQEr2uQX-x2EwQ/company-logo_100_100/0?e=1582761600&v=beta&t=aDbYLUDMvlS4DpwOLjOaQj3Dj60C_cYLC5UUvGoyld0" id="ember177" class="EntityPhoto-square-1 flex-shrink-zero ember-view">
<span id="ember178" class="text-align-left ml2 t-14 t-black t-bold full-width lt-line-clamp lt-line-clamp--multi-line ember-view" style="-webkit-line-clamp: 2"> THIS IS THE CITY AND STATE OF COMPANY
<!----></span>
</a>
</li>
</ul>
</div>
The xpath string is picking up the two span elements using class. I can't use the span id attributes because they are dynamic and change with each page (one page per company).
Can someone advise how I extract the desired text?
Thanks.
point to the li level.
//ul/li[2]/a/span[starts-with(#class,"text-align")]

Scraping text within several span tags (Ruby & Nokogiri)

I am trying to scrape "Description" from this HTML structure
<div class="menu-index-page__item-content">
<h6 class="menu-index-page__item-title">
<span> Item title </span>
</h6>
<p class="menu-index-page__item-desc">
<span>
<span>
<span>Description</span>
</span>
</span>
Each tag has an element with it that I don't know how to handle:
data-reactid=".3wrqgx5340.3.5.0.4:$523105.2.$3959254.$menuItemContent.1.0"
Each data-reactid is different. So if I target this attribute I will scrape stuff I don't want.
I've tried .search .xpath, using tags and classes but nothing seems to work.
Is there a way to say: give me the p tag that has a class="menu-index-page__item-desc" and scrape the 3rd span from there?
You can get the required value via xpath
//text()[contains(.,'Description')]
You code and xpath:

How to use XPath extract text without Html tag?

<div id="info" class="">
<span>
<span class="pl"> author</span>:
<a class="" href="/search/author"Peter</a>
</span><br/>
<span class="pl">publisher:</span> god cor<br/>
<span class="pl">year:</span> 2011-6<br/>
<span class="pl">page:</span> 360<br/>
<span class="pl">price:</span> 39.50<br/>
From the above HTML tags, i want to extract those numbers with XPath.How can i do that?
Thanks.
The XPath for each number is (in order as shown above) :
//*[#id="info"]/a/text()[2] --> 2011-6
//*[#id="info"]/a/text()[3] -->360
//*[#id="info"]/a/text()[4] --> 39.5
You can know the XPath for any tag by just opening the html file in Chrome, right clicking on the view and choosing "inspect". When you find the tag you want, just right click on it and choose Copy-> Copy XPath.

Xpath: Select parent link matching three child attributes

I need to select a link using xpath that matches the following three criterion:
parent #class = 'testItem'
child #class = 'icon icon_checked'
text = 'test text goes here!'
i'm unsure about where to put the text attribute in the xpath reference. i've tried many permutations of the following:
//a[#class="testItem" and child::span[#class="icon icon_checked"] and li[text()="test text goes here!"]]
my issue is that the text part is not in its own span.
here's the raw example:
<li>
<a class="testItem2" data-code="2" href="javascript:void(0);">
<span class="icon icon_checked"></span>
test text goes here2!
</a>
</li>
<li>
<a class="testItem" data-code="2" href="javascript:void(0);">
<span class="icon icon_checked"></span>
test text goes here!
</a>
</li>
Thanks for the help. I've found the answer.
I can simply change the last part of my xpath from li[text()="test text goes here!"] to .[text()="test text goes here!"]].
My final working xpath is:
//a[#class='testItem' and child::span[#class='icon icon_checked'] and .[text()='test text goes here!']]

How to click on anchor <a> tag from Canoo webtest?

Our team replaced a submit button with the following anchor tag:
<a class="l-btn" onclick="onFormSubmit(this.form)" Style="width: 80px; text-align:center;">
<span class="l-btn-left">
<span class="l-btn-text">Save</span>
</span>
</a>
I retrieved xpath from XPather, and tried to use from canoo webtest to click on it, but received "failed: Link not found".
Does anyone know how to simulate click on the above?
Thanks.
<clickLink xpath="//a[#class='l-btn']" description="Click first a tag with class l-btn" />
See clickLink documentation
clickLink xpath="//a[contains(#onclick,'onFormSubmit')]
description="click link with onclick property that contain the word
onFormSubmit"
will look for the link with xpath property onclick that contain a value 'onFormSubmit'

Resources