How can I retrieve a set of links present as list items in an ordered list using XPath? I need to retrieve all sections from a forum with the following html code:
<div id="pagewrapper" class="fixed">
<div id="toplinks" class="toplinks" style="position: relative; top: 145px;">
<div class="above_body" style="height: 210px;">
<div class="body_wrapper">
<div id="breadcrumb" class="breadcrumb">
<div id="pagetitle">
<ol id="forums" class="floatcontainer">
<li id="cat3" class="forumbit_nopost new L1">
<div class="forumhead tcat foruminfo L1 collapse">
<div class="tbody_left">
<div class="tbody_right">
<ol id="c_cat3" class="childforum">
<li id="forum9" class="forumbit_post new L2">
<div class="forumrow table">
<div class="foruminfo td" style="padding-top: 12px; padding-bottom: 12px;">
<img id="forum_statusicon_9" class="forumicon" alt="" src="elitex360/statusicon/forum_new-48.png">
<div class="forumdata">
<div class="datacontainer">
<div class="titleline">
<h2 class="forumtitle">
Forum index <!-- get this link -->
</h2>
</div>
<p class="forumdescription">
</div>
</div>
</div>
<h4 class="nocss_label">Forum Actions:</h4>
<h4 class="nocss_label">Forum Statistics:</h4>
<ul class="forumstats td" style="padding-top: 18px; padding-bottom: 12px;">
<div class="forumlastpost td">
</div>
</li>
<li id="forum22" class="forumbit_post new L2">
<li id="forum40" class="forumbit_post new L2">
</ol>
<div class="tbody_under"></div>
</div>
</div>
<div class="tfoot">
</li>
<li id="cat4" class="forumbit_nopost new L1">
<li id="cat52" class="forumbit_nopost new L1">
<li id="cat5" class="forumbit_nopost new L1">
<li id="cat6" class="forumbit_nopost new L1">
<li id="cat7" class="forumbit_nopost old L1">
</ol>
The section links I have to retrieve are marked in the code above (<!-- get this link -->). I am now using the following string to retrieve all list items:
//div[#id='pagewrapper']/div[3]/ol
retrieving all list items. But I don't know how to "enter" each list item and retrieve the link label's contents. In the examples I found, knowledge of the number of list items is necessary prior to accessing them. That is not the case as the forum may have a different number of list items (the template is for a forum engine, not a forum in particular).
How can I retrieve all links within the list items?
Try below xpath to get the URL:-
//a[contains(.,'Forum index')]/#href
If you want all li in ol as I understand from you question then the xpath is as below:-
//div[#id='pagewrapper']//li[#id='cat3']//ol//li
I think Below is the xpath you are expecting:-
//div[#id='pagewrapper']//div/#href
Hope it will help you :)
Related
I'm pretty new in Selenium and UI automation. Have some problem with clicking on a dynamic element inside span class. So this id everytime changes for each of 3 drop-down elements. So each class for each this element the same which is create the problem as well.
So I need change the value for id="react-select-2585057--value-item"
<div class="field loan-selection">
<label class="field__body">
<div class="field__label">Verwendung
<!-- -->
</div>
<div class="field__control">
<div class="Select customSelect has-value Select--single">
<div class="Select-control">
<span class="Select-multi-value-wrapper" id="react-select-2585057--value">
<div class="Select-value">
<span class="Select-value-label" role="option" aria-selected="true" id="react-select-2585057--value-item">Freie Verwendung</span>
</div>
<div aria-expanded="false" aria-owns="" aria-activedescendant="react-select-2585057--value" aria-disabled="false" class="Select-input" role="combobox" style="border:0;width:1px;display:inline-block" tabindex="0"></div>
</span>
<span class="Select-arrow-zone">
<span class="Select-arrow"></span>
</span>
</div>
</div>
</div>
</label>
</div>
As per the HTML to invoke click() on the element with dynamic id as id="react-select-2585057--value-item" assuming this element will always be a descendent of the node <div class="field__label"> you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='field loan-selection']//div[#class='field__label' and contains(., 'Verwendung')]//following::div[1]//span[#class='Select-value-label' and starts-with(#id,'react-select-')]"))).click();
I need parse data from web page by tag ("p"). I try like this:
Elements content = document.getElementsByTag("p");
for(Element el : content) {
System.out.println(el.text());
}
And it's work fine. But I get superfluous data.
For example:
<div class="DicCellTerm">
<h1>Impossible</h1>
<div class=des>
<p class=par2><span class=hint><em>smth</em></span></p>
<p class=par2>1) (<em>with</em>) all, do</p>
<p class=par2>2) <span class=hint><em>text</em></span> some words</p>
<p class=par3>it is impossible</p>
</div>
</div>
</div><!--DicCell end-->
<div align="center" class="AdContent" id="adcontentnoprint">
<div class=SharedItems>
<div class=DicCellParent>
<span class=LinkOtherDic>+ dictionary <strong>impossible</strong> - translate</span>
<div class=DicCellOther id=diccellothershow>
<h2>impossible</h2>
<div class=des>
<p class=par1>1) important, is</p>
<p class=par1>what</p>
<p class=par1>2) true, false</p>
</div>
</div>
<!--DicCellOther end-->
</div>
<!--DicCellParent end-->
<div class=DicCellParent>
<span class=LinkOtherDic>+ translate <strong>important</strong> - dictionary</span>
<div class=DicCellOther id=diccellothershow>
<h2>importnant</h2>
<div class=des>
<p class=par1>1) müim, emiyetli; emiyet bar</p>
<p class=par1>it is very important - bu pek müimdir, bunıñ büyük emiyeti bar</p>
<p class=par1>2) qopayıp, qabarıp</p>
</div>
</div>
<!--DicCellOther end-->
</div>
<!--DicCellParent end-->
</div>
<!--SharedItems end-->
I need to get data by tag "p" before class SharedItems.
I tried parse data by class "DicCellTerm" and I get properly data. And all data is written in one line, but I need to get data as on web page.
Elements elements = document.select(".DicCellTerm p");
This grabs all p inside the .DicCellTerm class, then you can iterate over elements. Here is a link to all possible selectors in jsoup, this is where i get most of my help =)
https://jsoup.org/apidocs/index.html?org/jsoup/select/Selector.html
On users.html, there is the main and industries tab.
When I load users.html I want industries tab to load. how do I do it?
<div class='tabs-x tabs-above tabs-krajee'>
<ul id="myTab-tabs-above" class="nav nav-tabs" role="tablist">
<li class="active">
Main</li>
<li>Industries</li>
</ul>
<!-- Tab content refers to this part -->
<div id="tabs-content" class="tab-content">
<div id="home-tabs" class="tab-pane fade in active">
<div th:include="fragments/admin/companyProfile::companyProfile(${currentAccountant})"></div>
</div>
<div id="users-tabs" class="tab-pane fade">
<div th:include="fragments/admin/users::users">a</div>
</div>
</div>
</div>
Following code loads userDetails but I want a specific tab to be selected on loading. How do I do it?
#RequestMapping(value = "/load", method = RequestMethod.GET)
public ModelAndView load() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("userDetails");
return modelAndView;
}
You need to trigger tab show on document load.
Check documentation
<div class='tabs-x tabs-above tabs-krajee'>
<ul id="myTab-tabs-above" class="nav nav-tabs" role="tablist">
<li class="active">
Main</li>
<li>Industries</li>
</ul>
<!-- Tab content refers to this part -->
<div id="tabs-content" class="tab-content">
<div id="home-tabs" class="tab-pane fade in active">
<div th:include="fragments/admin/companyProfile::companyProfile(${currentAccountant})"></div>
</div>
<div id="users-tabs" class="tab-pane fade">
<div th:include="fragments/admin/users::users">a</div>
</div>
</div>
</div>
<script th:inline="javascript">
var tabToSelect = "users-tabs";
$(document).ready(function() {
$('#myTab-tabs-above a[href="#'+tabToSelect+'"]').tab('show')
});
</script>
You are not specified that which tab to select. How do you want to separate the logic for adding Main and industries.
In Controller
if you want to show MainTab
modelAndView.addObject("mainTabAddOrNot","show");
otherwise
modelAndView.addObject("mainTabAddOrNot",null);
In Thymeleaf side:
<li th:if="$(mainTabAddOrNot)" >
Main</li>
<li>Industries</li>
It is just an example to give an idea to use conditions in thymeleaf for specific tag adding. Hope it will give you an idea.
There is no need to change your controller code neither javascript. Just set your tab active .
Also you must set the Industries tab active.
<a class="list-group-item list-group-item-action active" data-toggle="list" href="#industries"
role="tab">
Industries Tab Data
</a>
<div class="tab-pane fade show active" id="industries" role="tabpanel">
I have below HTML code, where I have different <'li' class> tags under the <'ul'> tag. Within the <'li'> class tag, I will have tag which needs to be clicked. These are results of some search in webpage, where all the items will be displayed as a list with pagination.
I need to verify if this exist? and if it exists in 3 or 4th Index on the page, I need to click that link. Please let me know how I can get all these items into array and loop them and click any product available at random index.
<ul id ="e1ResultsList">
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<div class="banner">
<div class="text">High Offer</div>
</div>
</div>
<span class="e1ListingImages">
<a class="`jsListingURL`" `href`="/event?`eventid`=7844">
<div class="e1SellerLogo e1Hidden"></div>
</span>
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/event?`eventid`=7844">vehicles and buses</a>
</div>
<span id="7844" class="" style="width: 100%;">enter code here
<div class="e1EventDetails">
<span class="e1EventInfo">
<div class="e1EventDescription">
<div class="e1EventID">
<div class="e1ListingCount">
<div class="e1ClosingDate">
<div class="e1EventHighlights">
</span>
</div>
</span>
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<span id="7846" class="" style="width: 100%;">
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon ribbon-white">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=310786">2009 International 9200i Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="310786" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=312207">2009 Mack CXU613 Vision Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="312207" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>
</ul>
You could get a List of the 'a' tag WebElements on the page using the following:
List<WebElement> aTagsList = driver.findElements(By.tagName("a"));
You can then iterate over aTagsList as needed.
Edit:
To click the 4th 'a' tag WebElement in aTagsList, you can use the following:
WebElement requiredElement = aTagsList.get(4);
requiredElement.click();
Is it always the 4:th item you want to press?
If not, you have to know what you want to click. Either it is depending on the text or the "listingid".
driver.findElements(By.cssSelector("a[href*='310786']"));
or
driver.findElement(By.cssSelector("a:(*'2009 International 9200i Truck Tractor'*)")); );
<div id="sbGlobalNav" class="">
<div id="sbGlobalNavContent">
<div id="sbGlobalNavRightContent">...</div>
<div id="sbGlobalNavLeftContent">
<ul id="globalNavMenu">
<li class="globalNavSeparator firstSeparator">..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li>..</li>
<li class="globalNavDropdown">
<span>Answer</span>
<ul class="globalNavDropdownContainer" style="display: none;">
<li>..</li>
<li>Daily Polls</li>
<li>..</li>
</ul>
</li>
<li class="globalNavDropdown">..</li>
<li class="globalNavSeparator">..</li>
<li class="globalNavDropdown" id="globalNavRewards">..</li>
<div class="clear"></div>
In this I need to find and click Daily Poll link using Selenium webdriver(java) but I was unable to do it.
What I did is:
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.xpath("//div[#id='sbGlobalNav']//span[text()='Answer']"))).build().perform();
driver.findElement(By.linkText("Daily Polls")).click();
Check it, whether you have to so something to trigger that element out. Javascript maybe, by mouse over ?
Try this:
driver.findElement(By.xpath("//*[text()='Daily Polls']")).click();
It might work.