Select href with id and class using xpath - xpath

let say I have DOM like this:
<div id="tabsmenu">
<ul>
<li class="one">foo</li>
<li class="two">baz </li>
</ul>
</div>
and I would like to get the text from <a href> elements:
# desired output: ['#foo', '#baz']
How to do it using xpath and using combination id and element with a specific class within id ?
Already tried:
some_doc.xpath('//a[#id="tabsmenu"]/[#class="ui-tabs-anchor"]/#href')
# select all href tags of any a element that is in id tabsmenu and class attribute ui- tabs-anchor
EDIT - corrected tabmenu into tabsmenu

You're most likely looking for something like this:
//div[#id='tabsmenu']//a[#class='ui-tabs-anchor']/#href
That will get all href attributes that are part of an a tag with the class ui-tabs-anchor and inside a div element with the id tabsmenu.
Also you might want to take a look at this question:
Find out if class name contains certain text
This is because the class will match the exact value (ui-tabs-anchor) and maybe some additional class might be added there such as class="ui-tabs-anchor disabled" and then there will not be a match in there.

Related

fetching all the data from a partially hidden list watir

I'm trying to fetch a span of 4,600 elements
<span> 4,600 </span>
i inspected the elements and found that each element is a list class which has a child class with a title and href that i want to fetch the problem is that :
not all elements are visible , you would have to scroll down the api to find more elements
i cant seem to successfully fetch a single piece of data
puts browser.th(:class => %w("_9irns _pg23k _jpwof _gvoze")).link.hreflang
this is the structure of the code i'm trying to fetch
<ul class = 'xxx'>
<div class = 'xxa'>
<li class ='fff'>
<li class ='fff'>
<li class ='fff'>
.
.
the <li class = 'fff'> has <a class='xxx xxx xxx xxx'> having the data i'm trying to fetch tittle and href
to be more clear how can I iterate over all the classes of 'fff' and pick a url which is in a child class of it.
Don't use quotes inside the %w to find an element from a collection of classes, and try requiring watigiri gem and using #text! to obtain text of hidden elements.

How to remove class from all list of an id with prototypejs

I want to remove all classes named active of all <li> under <ul id="navNovelty"> using prototypejs.
I did $('ul#navNovelty li').removeClassName('active'); but it did not work. Here is my full code
<ul id="navNovelty" class="nav om">
<li id="multimedia" class="active">Multimedia</li>
<li id="love">Love & Family</li>
<li>MyBusiness</li>
</ul>
How to remove class from all list of an id with prototypejs ?
Unlike jQuery, Prototype has a different accessor for single elements and multiple elements. If you want to target a single item, and find it by its id, you use one dollar sign:
$('navNovelty').removeClassName('active');
If you want to access multiple items, you use two:
$$('ul#navNovelty li').map('removeClassName', 'active');

Scraping the href value of anchor in Ruby

Working on this project where I have to scrape a "website," which is just a an html file in one of the local folders. Anyway, I've been trying to scrape down to the href value (a url) of the anchor tag for each student object. I am also scraping for other things, so ignore the rest. Here is what I have so far:
def self.scrape_index_page(index_url) #responsible for scraping the index page that lists all of the students
#return an array of hashes in which each hash represents one student.
html = index_url
doc = Nokogiri::HTML(open(html))
# doc.css(".student-name").first.text
# doc.css(".student-location").first.text
#student_card = doc.css(".student-card").first
#student_card.css("a").text
end
Here is one of the student profiles. They are all the same, so I'm just interested in scraping the href url value.
<div class="student-card" id="eric-chu-card">
<a href="students/eric-chu.html">
<div class="view-profile-div">
<h3 class="view-profile-text">View Profile</h3>
</div>
<div class="card-text-container">
<h4 class="student-name">Eric Chu</h4>
<p class="student-location">Glenelg, MD</p>
</div>
</a>
</div>
thanks for your help!
Once you get an anchor tag in Nokogiri, you can get the href like this:
anchor["href"]
So in your example, you could get the href by doing the following:
student_card = doc.css(".student-card").first
href = student_card.css("a").first["href"]
If you wanted to collect all of the href values at once, you could do something like this:
hrefs = doc.css(".student-card a").map { |anchor| anchor["href"] }

How to handle dynamic Id in selenium?

I am working on automation and getting dynamic id each and every-time .Please find the sample code..
<li class="list-group-items pingMessage clearfix" id="59580" data-reactid=".c.$0.0.$59580"></li>
<li class="list-group-items pingMessage clearfix" id="59581" data-reactid=".c.$0.0.$59580"><li>
<li class="list-group-items myMessage pingMessage clearfix bunch" id="59588" data-reactid=".e.$0.0.$59578"></li>
Every <li> item has a different id. I want to get an item's id value using gettext then store it in a variable and call in xpath.
I tried following code :
//Getting id of particular text
WebElement Id=driver.findElement("By.xpath(//*[#class='list-group-items myMessage pingMessage clearfix bunch']").getText();
//My problem starts here i want to pass the stored id as id value how can i do it..
driver.findElement("By.id("+Id+")).click;
getText() is used to get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace while you need here to get element id attribute, So you should try using getAttribute() as below :-
//Storing the value of ID
WebElement Id = driver.findElement(By.cssSelector(".list-group-items.myMessage.pingMessage.clearfix.bunch")).getAttribute("id");
Please try with below xpath
//tagName[contains(#id='595')]

Xpath expression to get a href description of a given class name

in this div class:
<div class="black">
Vitamin
Watergate
</div>
I need an Xpath expression to get just the a href tag Description text, in this example "Vitamin" and "Watergate".
/div[#class='black']/a[0]/text()
will return Vitamin and
/div[#class='black']/a[1]/text()
will return Watergate.
Regards
You better google that first.

Resources