I have the elements, highlighted in image :
The xpath for the list is //div[#class='list-group formsFilteredList']//a[#class='list-group-item'], but I need the text of that a tag. The text of that are changes dynamically. So I cannot use text contains method
Try this:
//div[#class='list-group formsFilteredList']//a[#class='list-group-item']/text()
You could find the element by class name instead of xpath but there are several elements with the same class name so what you could do is get the elements into a list and work with them from there:
List<WebElement> lstElements = driver.findElements(By.className("list-group-item"));
Then if you know where in the list the element is that you want to use, you can specify the index. For example if you want to click the second item in the list you'd do this:
lstElements.get(1).click();
If you want to do something with each of the items in turn then iterate through them in a for loop:
for (int i = 0; i < lstElements.size(); i++ ) {
System.out.println("List item text: " + lstElements.get(i).getText());
}
Related
I am trying to write automation script to Identify node index of list, with link text ‘Setup First Project’ using xpath and Get the text associated with link node next to above.
To Locate element use below line.
WebElement link = driver.findElement(By.xpath("//a[text()="Setup First Project"]"));
To get text from element use getText() method.
String text= link.getText();
Use the below xpath:-
//a[text()="Setup First Project"]
If you care for speed and you can deppend on the <section id="recent-posts-3", use this xpath:
//section[#id='recent-posts-3']/ul/li/a[text()="Setup First Project"]
I wrote script to save all links and then print index of required link text.
//Save all links available on the page as List
List<WebElement> links = driver.findElements(By.xpath("//a"));
int setupIndex=0;
for(int i =0;i<links.size();i++) {
String elementText = links.get(i).getText();
if(elementText.equals("Setup First Project"))
{
setupIndex =i;
}
System.out.println(elementText);
}
// 5.2 Identify node index of list, with link text ‘Setup First Project’
System.out.println("the index is"+ setupIndex);
//5.3 Get the text associated with link node next to ‘Setup First Project’
System.out.println(links.get(setupIndex+1).getText());
}
Iam trying to locate elements in a webpage. Elements are arranged in rows. For all the rows(or elements), the common attribute is "pfobj". With the help of list interface i have listed out all the elements having attribute pfobj. While finding element with xpath iam getting error. I tried to insert srting pfobj inside xpath argument.
code for a particular element:
WebElement eachelement = driver.findElement(By.xpath("//*[#pfobj=\"pfobj1\"]"));
I want to run for all elements by using loop. so i need to insert "pfobj"(which will iterate by increasing the value) in the place of "pfobj1".
I have tried the several ways but iam getting error:
String slash1 = "\\";
String pfobj = pfobj1;
String slash2 = "\\";
String final = slash1 + pfobj + slash2
Can someone please help me out with this issue
You can try this code :
for ( WebElement element: List) {
if (element.getAttribute("pfobj").equals("pfobj")){
// do what you want
}
}
List is your element list
From what I take from this question is that there are several elements in your HTML having value of pfobj attribute as pfobj1, pfobj2, and so on.
If you want to compare all such elements in a loop, you can do like -
List<WebElement> your_list = driver.findElements(By.xpath("//input[contains(#pfobj,'pfobj')]"))
int counter = 1;
for(WebElement your_element : your_list )
{
if(your_element.getAttribute("pfobj").equals("pfobj" + counter))
{
//do whatever you want
}
counter++;
}
I need to select all the checkboxes from here one-by-one after every 3 second. I tried couple of xpaths with list,none of them have worked
Tried xpaths:
//div/div[#class='filters-list sdCheckbox ']
Using input and type. But none of them worked. Can you please help me out?
Reference website: https://www.snapdeal.com/products/storage-devices?sort=plrty
->Capacity at the left hand corner
By.xpath("//a[#class='filter-name']") this one listed out all the filters of page.
The xPath "//div[#data-name='Capacity_s']/div[#class='filters-list sdCheckbox ']/input" will fetch you the list of all input elements that you need to check.
There is a container DIV that holds all the filters of a certain type, e.g. Brand, Capacity, etc. The one for Brand is shown below.
<div class="filter-inner " data-name="Brand">
Under that container, all the LABEL tags are what you need to click to check the boxes. So, we can craft a CSS selector using the grouping as a filter to reach only the checkboxes we want.
"div[data-name='Brand'] label"
Since this is something I assume you will reuse, I would write this as a function.
public static void CheckFilters(String filterGroup)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> filters = driver.findElements(By.cssSelector("div[data-name='" + filterGroup + "'] label"));
// System.out.println(filters.size()); // for debugging
for (int i = 0; i < filters.size(); i++)
{
filters.get(i).click();
// wait for the two overlays to disappear
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.searcharea-overlay")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.filterLoader.hidden")));
// reload the element list after the refresh so you don't get StaleElementExceptions
filters = driver.findElements(By.cssSelector("div[data-name='" + filterGroup + "'] label"));
}
}
and you would call it like
driver.get("https://www.snapdeal.com/products/storage-devices?sort=plrty");
CheckFilters("Brand");
I want to send values for particular field
I have found Password locaters value is same on this website.
How to handle same objects which have same id,name,tagname etc.?
You can find them like that :
First one :
driver.findElement(By.xpath("//form[#id='loginForm']/div/div/input[#id='password']")
Second one :
driver.findElement(By.xpath("//form[#id='registration-form']/div/div/input[#id='password']")
With that solution, you'll be sure to find the right element even if an other id=password is added before the one you are looking for.
You can use List<WebElement> to create a list of elements with "same id,name,tagname" and perform indexed operations as :
List <WebElement> elements = driver.<your locator strategy>;
for (int i = 0; i< elements.size(); i++) {
elements.get(0).<perform your action>;
}
There are some span tags in website:
<span class="valuearea">Text</span>
I need to get all values between these tags and add them to array or list.
driver.find_element_by_class_name("valuearea") finds first span tag and assigns to variable this tag. (variable.text shows Text between first span tags).
So, How to make it for all variables?
In python
results = []
elements = driver.find_elements_by_tag_name("span")
for element in elements:
results.append(element.text)
in java
ArrayList<String> result = new ArrayList<String>();
List<WebElement> temp = driver.findElements(By.className("valuearea")) // a list of elements with classname="valuearea"
for (WebElement ele : temp){
result.add(ele.getText());
}