I am using selenium to select an xpath based on the date but there are two separate dates in the html both that start with td. How do I specify which date to select? Do I place a [2] at the end of the search text?
The line looks like this
List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')]/preceding::*5[]"
so should I do this
List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "'[2])]/preceding::*5[]"
List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')]"))
you are using list do you can get the element get (list index starts from 0 )
WebElement li = li2.get(4)
or
you can get 5th result of xpath by enclosing the xpath locator with brackets and calling [5] ( here index starts from 1 )
WebElement li = driver.findElements(by.xpath("(//td[contains(text(),'" + date " "')])[5]"))
If you want to get 5th sibling then don't enclose it with bracket , this will say get the 5th td tag that is a sibling and contains the date
WebElement li = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')][5]"))
To answer my question.
List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')]/preceding::*5[1]"
Would be how to index this particular xpath.
Related
How to capture search result count.It varies each time I had tried with
int result = driver.findelements(by.xpath("")).size(); but always I'm getting 0. XPath is correct.
Code in website:
<div class="search-msg-count ng-binding">Search result count: 100 entries</div>
Capture the text from the Element with the xpath -
WebElement e = driver.findElement(By.xpath("//div[#class='search-msg-count ng-binding']"));
String elementText = e.getText().trim();
Then split the string with ":" -
String split[] = elementText.split(":");
Your required text will be in second array element. Get it & replace entries word from it.
String finalValue = split[1].replace("entries","").trim();
System.out.println(finalValue);
String str = "some value" ;
getTestBase().getDriver().findElement(By.xpath("//h2[.='" + str + "']//parent::div//div[#id='sectionList'][1]/section/div/button")).click();
I need to put the above in loop so that [1] keeps increasing every time , how can I update the above xpath for the integer ?
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
for(int i=0; i<5; i++){
driver.findElement(By.xpath("//h2[.='" + str
+"']//parent::div//div[#id='sectionList']["+i+"]/section/div/button")).click();
}
This should solve your problem. 5 is just an arbitrary limit i assumed for writing the solution.
On the other hand you can try and get all the elements and store the elements in a list. Then parse the list to get the the buttons and click on them.
Some thing like:
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
List<WebElement> listOfButtons = driver.findElements(By.xpath("//h2[.='" + str + "']//parent::div//div[#id='sectionList']//section/div/button""));
for(WebElement el: listOfButtons){
el.click();
}
If I'm expecting a page to display 5 offers, how would I tell webdriver to list all those 5 offers in a ul?
The Ul html code is
<ul id="more-load" class="product_list_widget pagination-centered" style="padding-top:15px;">
I think you would use
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='more-load']/ul/li"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
but I'm not sure how to print each individual offer in the Ul and match the 5 offers expected to be displayed
EDITED CODE
never mind used this and worked
WebElement allElements = driver.findElement(By.id("more-load"));
List<WebElement> liElements = allElements.findElements(By.tagName("li"));
for (int i = 0; i < liElements.size(); i++)
{
System.out.println("-------------------------------------------------------");
System.out.println(liElements.get(i).getText());
}
however if I have a column on the left side, with ul = product categories, how would I loop it to go through each individual link text and preform the same function
Let's say your 5 offers are as below:
Offer1
Offer2
Offer3
Offer4
Offer5
now your approach is correct till for loop:
then you can do something like :
String offerString = Offer1 + " " + Offer2 + " " + Offer3 + " " + Offer4 + " " + Offer5;
for (WebElement element: allElements) {
if(offerString.contains(element.getText()){
System.out.println("Offer item is: " + element.getText());
}
}
Stick with your original attempt, except change the line:
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='more-load']/ul/li"));
to
List<WebElement> allElements = driver.findElements(By.xpath("//ul[#id='more-load']/li"));
using java8:
List<String> offers = driver
.findElements(By.cssSelector("#more-load li"))
.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
and you can assert offers.size() to have some expected value by any assert library
To get linked text elements in UL you can use next selector:
driver.findElements(By.cssSelector("ul a"))
My HTML code consists of multiple divs. Inside each div is a list of anchor tags. I need to fetch the href values and text values of the anchor tags that are in the sub-container div. I'm using Selenium to get the HTML code of the webpage.
HTML code:
<body>
<div id="main-container">
One
Two
Three
<div id="sub-container">
Abc
Xyz
Pqr
</div>
</div>
</body>
Java code:
List<WebElement> list = driver.findElements(By.xpath("//*[#href]"));
for (WebElement element : list) {
String link = element.getAttribute("href");
System.out.println(e.getTagName() + "=" + link);
}
Output:
a=www.one.com
a=www.two.com
a=www.three.com
a=www.abc.com
a=www.xyz.com
a=www.pqr.com
Output I need:
a=www.abc.com , Abc
a=www.xyz.com , Xyz
a=www.pqr.com , Pqr
Try this,
List<WebElement> list = driver.findElements(By.xpath("//div[#id='sub-container']/*[#href]"));
for (WebElement element : list) {
String link = element.getAttribute("href");
System.out.println(element.getTagName() + "=" + link +", "+ element.getText());
}
You can use element.getText() to get the link text.
If you only want to select the links in the sub-container, you can adjust your xPath:
//*[#id="sub-container"]/a
Pretty simple, try as below:
`List<WebElement> list = driver.findElements(By.xpath("//div[#id='sub-container']/a"));
for (WebElement element : list) {
String link = element.getAttribute("href");
String text = element.getText();
System.out.println(e.getTagName() + "=" + link + ", " + text);
}
if id sub-container is unique, just use the below line
driver.findElements(By.cssSelector("div#sub-container>a"));
thanks
I have the following jsFiddle
http://jsfiddle.net/B5zvV/
I am trying to use JSoup to obtain the value of the hyperlink's href string on Line 238:
<a href="/chain/admin/config/editRepository.action?planKey=AB-CSD&repositoryId=28049450">
Hence, the desired result would be to obtain a String with a value of:
/chain/admin/config/editRepository.action?planKey=AB-CSD&repositoryId=28049450
Here's my code:
Document doc = Jsoup.connect("http://myapp.example.com/fizz.html").get()
Elements elems = doc.getElementsByAttributeValueContaining("href", "repositoryId")
When I run this, the value of elems is empty: why, and what do I need to do to get the desired String?
The getElementsByAttributeValueContaining() method will return multiple values in this case because many hrefs has repositoryId. If you are particular about line 238 then that a is enclosed inside an li with class item item-default. There is only one such li and two a tags inside it. Just take the first one like
String html = "<li class=\"item item-default\" data-item-id=\"28049450\" id=\"item-28049450\">"
+ "<a href=\"/chain/admin/config/editRepository.action?planKey=AB-CSD&repositoryId=28049450\">"
+ "<h3 class=\"item-title\">MCAppRepo <span class=\"item-default-marker grey\">(default)</span></h3>"
+ "</a>"
+ "<a href=\"/chain/admin/config/confirmDeleteRepository.action?planKey=AB-CSD&repositoryId=28049450\" class=\"delete\" title=\"Remove repository\">"
+ "<span class=\"assistive\">Delete</span>"
+ "</a>"
+ "</li>";
Document doc = Jsoup.parse(html);
Elements elems = doc.select("li.item.item-default > a");
System.out.println(elems.first().attr("href"));