Selenium - locating multiple elements with the same class name - selenium

Hello there I am trying to locate multiple elements with the same className. The className and the body structure of the elements are the same but the texts, links and pictures are different.
<div class="dc-content-right clearfix"> (parent)
<div class="dc-item clearfix"> (child nodes)
<div class="dc-item clearfix">
<div class="dc-item clearfix">
Each of these child elements looks something like this:
<div class="dc-item clearfix">
<div class="dc-icon">
<div class="dc-info">
<h2>
AVG AntiVirusFree 2015
</h2>
Each child element has different text in the H2 tag. So once it is AVG AntiVirus Free 2015, then it is Internet Security .... and so on. So what I want to do is save all elements into a list and then work with them.
At first I save these elements intto a list of WebElements :
List <"WebElement"> list = driver.findElements(By.xpath("//div[#class='dc-item clearfix']"));
Afterwards I want to iterate through the list and write the h2 text for each element on the screen:
for(WebElement i:superDiv)
{
System.out.println(i.findElement(By.xpath("//h2/a")).getText());
}
So the outcome should be a list of 3 different headings extracted from divs. The problem: the outcome is the list of 3 headings which are the same!
AVG AntiVirus Free 2015
AVG AntiVirus Free 2015
AVG AntiVirus Free 2015
It looks like I located the same element 3 times. Does anyone have an idea what could be the problem? Thank you

List<WebElement> list = driver.findElements(By.xpath(".//*[#class='dc-item clearfix']//h2/a"));
for(WebElement el : list) {
System.out.println(el.getText());
}

You can also try:-
List<WebElement> list = driver.findElements(By.xpath(".//*[#class='dc-info']//a"));
for(WebElement element : list) {
System.out.println(element.getText());
}

If div tag does not contains id or class, then use like this
List<WebElement> elements = driver.findElements(By.xpath("//div[contains(#style,'height:50px')]"));
for(WebElement element : elements) {
System.out.println(element.getText());
}

Related

Xpath following the for the next thumbnail - optimized solution? (for selenium automated solution)

At demo store we have a list of thumbnail as given below :
<ul class="product_list grid row">
<li class="ajax_block_product....">
<div class="product-container">
<!-- at this level ist the thumbnail number 1 -->
</div>
</li>
<li class="ajax_block_product....">
<div class="product-container">
<!-- at this level ist the thumbnail number 2 -->
</div>
</li>
<li class="ajax_block_product....">
<div class="product-container">
<!-- at this level ist the thumbnail number 3 -->
</div>
</li>
</ul>
I can navigate through thumbnails using the xpath mentioned below:
thumbnail number 1 = //div[#class="product-container"]
thumbnail number 2 = //div[#class="product-container"]/following::div[#class="product-container"][1]
thumbnail number 3 = //div[#class="product-container"]/following::div[#class="product-container"][1]/following::div[#class="product-container"][1]
Through, above xpath are working fine for me but not an optimized solution.
Update 1: The objective is leave the xpath in a closed "form" at web page object library, for using by automated tests.
Get all the thumbnails in a list and then navigate through. Use below CSS selector for the same. Not sure which language you are using. I write this using Java.
List<WebElement> products = driver.findElements(By.cssSelector("div.product-container"));
for(WebElement product : products){
String productName = product.findElement(By.cssSelector(".product-name")).getText();
String productPrice = product.findElement(By.cssSelector(".right-block .price")).getText();
...
}
OR using the xpath mentioned by you.
List<WebElement> allThumbnails = driver.findElements(By.xpath("//div[#class='product-container']"));
for(WebElement thumbnails:allThumbnails){
String productName = product.findElement(By.xpath(".//a[#class='product-name']")).getText();
String productPrice = product.findElement(By.xpath("//div[#class='right-block']//span[#class='price product-price']")).getText();
...
}
UPDATED
As per your comment if it is require to mentioned fix element in your xpath then using indexes would be the right approach.
there are total 7 product present on the page you shared the URL and xpath so you can write it like-
thumbnail number 1 = //ul[#class='product_list grid row']/li[1]
thumbnail number 2 = //ul[#class='product_list grid row']/li[2]
...
thumbnail number 7 = //ul[#class='product_list grid row']/li[7]
If all the products share the same class you can just have Selenium pick up all matching elements, as opposed to explicitly stating one after the other like you're currently trying.
You've not said what language you're using, but here's what it'd look like in C#:
// Setup your web driver...
var thumbnails = driver.FindElements(By.XPath("//div[#class=\"product-container\"]"));
foreach (var thumbnail in thumbnails)
{
// Do work
}
Or
for(int i = 0; i < thumbnails.Count; i++)
{
// Do work by index
}
Note the plural FindElements, it returns a collection of all matching elements. Selenium does a fairly good job of matching methods across languages so that should point you in the right direction at least.

xpath selenium multi/and operators

Could anybody help how to solve this dilemma
I have this code:
<div>
Link
<span class="make">Chevrolet</span><br>
<span class="year">1956</span><br>
<span class="price">$20,000</span><br>
</div>
<div>
Link
<span class="make">Ford</span><br>
<span class="year">1958</span><br>
<span class="price">$21,000</span><br>
</div>
I need get the link for example Fords with the year greater then 1950.
Presently, I am using following xpath:
//*[text()='Ford' and .//text()>'1950']//parent::a
And this doesn't work! Have you any idea ?
This is one possible XPath :
//div[span/text()='Ford' and span/text()>1950]/a
Basically the XPath check if div has child span with text equals 'Ford' and another child span with value greater than 1950. Then from such div that match the two criteria above, return child a element.
demo
Better yet, only check span with class 'make' for manufacturer and span with class 'year' for manufacturing year :
//div[span[#class='make']='Ford' and span[#class='year']>1950]/a
You can write a generic method to get the required links, as shown below:
public static List<WebElement> getLinks(String linkText, int year) {
List<WebElement> links = driver.findElements(By.xpath("//a[text()='" + linkText + "']/following-sibling::span[1]"));
List<WebElement> linksGreaterThanRequiredYear = new ArrayList<WebElement>();
for (WebElement link : links) {
if (Integer.parseInt(link.getText()) > year)
linksGreaterThanRequiredYear.add(link);
}
return linksGreaterThanRequiredYear;
}
Hence, if you want to get the Fords with year greater than 1950, you can call above method in following way:
List<WebElement> fordsWithYearGreaterThan1950 = getLinks("Ford", 1950);
Above method can be further enhanced to include less than criteria as well. Let me know, if you have any further queries.

I am facing an issue in retrieving all the list items using selenium webdriver

Problem : I have a list of items inside a div class msb-container.
.There are 162 items in the list .I want to click the 150th item from the list .There also a scroll bar through which we can go doen to other elemenst and select it
How the html looks like :
<div class="mCSB_container" style="position:relative; top:0;">
<ul id="ul-countries">
<li>
<input id="country-3" type="checkbox" name="c:3">
<label for="country-3">Afghanistan</label>
</li>
<li>
<input id="country-6" type="checkbox" name="c:6">
<label for="country-6">Albania</label>
</li>
---other countries
</li>
</ul>
</div>
How my code looks like :
IList<IWebElement> countryList = driver.FindElements(By.XPath("//*[#id='ul-countries']/li"));
for (int i = 0; i <= countryList.Count; i++)
{
string temp = countryList.ElementAt(i).Text;
if (countryList.ElementAt(i).Text == "Brazil")
{
//do something
}
}
I am getting a correct count of 162 countries but i think they are not filled correctly .As when I try to retrieve the text from even the 15th country it gives me empty result .It only fills the text for those list item which can be seen on the screen .Although when I inspect element I can see all the required data in list item through html but not through my code .I tried to put the sleep to but no luck .
Please provide your inputs to solve the above issue.
|
Kindest Regards
IList<IWebElement> countryList = driver.FindElements(By.XPath("//*[#id='ul-countries']/li"));
for (int i = 0; i <= countryList.Count; i++)
{
string temp = countryList.ElementAt(i).findElement(By.CSS("label")).getText();
if (temp.equals("Brazil"))
{
//do something
}
}

Verify a text in the dropdown

I am trying to verify if the text is present in the dropdown menu. My assertion shows all the items, but with an error...expected[]but found[]. Please find below my script and the test failure message. Thanks in advance for the help!
My Script:
driver.findElement(By.cssSelector("#s2id_autogen4 > a.select2-choice > span")).click();;
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
expectedDropDownItems.add("keysearch");
expectedDropDownItems.add("short");
expectedDropDownItems.add("standard");
expectedDropDownItems.add("to-date");
Assert.assertEquals(expectedDropDownItems, driver.findElement(By.xpath(".//*[#id='select2-drop']/ul")).getText());
ERROR MESSAGE:
java.lang.AssertionError: expected [keysearch
short
standard
to-date] but found [[keysearch, short, standard, to-date]]
HTML:
<div style="top: 1973px; left: 261px; width: 500px; display: block;" class="select2-drop select2-with-searchbox select2-drop-active" id="select2-drop">
<div class="select2-search">
<input type="text" class="select2-input" autocomplete="off" tabindex="0">
</div>
<ul class="select2-results" style="">
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>keysearch</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>short</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>standard</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable" style="">
<div class="select2-result-label">
<span class="select2-match"></span>to-date</div></li></ul>
</div>
What you can do is,
1- First get all the elements of the dropdown.
2- Put it in a List
3- Iterate over the list, get texts of each element and then put it in a new ArrayList of Strings.
4- Assert using this ArrayList and the Original ArrayList of items.
Below code will help you out:
//Putting actual items in the Arraylist of expectedDropDownItems
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
expectedDropDownItems.add("keysearch");
expectedDropDownItems.add("short");
expectedDropDownItems.add("standard");
expectedDropDownItems.add("to-date");
//Iterating over expected items and printing
System.out.println("\n--- Expected Items");
for(int i=0;i<expectedDropDownItems.size();i++)
System.out.println(expectedDropDownItems.get(i));
System.out.println("\n");
ArrayList<String> actualDropDownItems = new ArrayList<String>();
//Getting the list of all items in dropdown
List<WebElement> elements = driver.findElements(By.xpath("//div[#class='select2-result-label']"));
//Iterating over the list of dropdown, getting text of each element and adding the text to the Arraylist of actualDropDownItems
for(WebElement element : elements){
System.out.println("Adding element: "+ element.getText());
actualDropDownItems.add(element.getText());
}
//Iterating over actual items and printing
System.out.println("\n--- Actual Items");
for(int i=0;i<actualDropDownItems.size();i++)
System.out.println(actualDropDownItems.get(i));
//Asserting the ArrayLists if they are equal or not
Assert.assertEquals(expectedDropDownItems, actualDropDownItems);
System.out.println("Asserted text match");
According to assertion i can suppose and suggest to do next on attempt to fix your problem (p.s. i'm not good at java, but according my exp in C#):
1. Try to print data returned from driver.findElement(By.xpath(".//*[#id='select2-drop']/ul")).getText() code and make sure that this data really equals to your data from arrayList.
2. If data seem equal, try another assertion methods (such as Assert.That(object, matcher) (e.g. in C# in could be Assert.That(new[]{1}, Is.EquivalentTo(new[]{1}));), Assert.arrayEquals etc)
If both arrays are equal and asserts not help, please provide sample of html and we will try to solve that problem in other way.

Selenium get dynamic ID XPath

I'm new on Selenium, new here and my english is not the best.
I'm using selenium with .NET ...
I have a HTML page like this but the number of the events are different:
<div id="eventContent" style="text-align: center;">
<div class="event" id="event-8971062">
<ul>
<li ...></li>
<li ...></li>
<li ...></li>
</ul>
</div>
<div class="event odd" id="event-9224880">
<ul>
<li ...></li>
<li ...></li>
<li ...></li>
</ul>
</div>
</div>
I need to check all datas in the different divs but the count is dynamic and the (event)id is dynamic too. I'm trying to find out the count of the divs at first but this does'nt work. For that I try this:
DefaultSelenium selenium = new DefaultSelenium(...);
decimal count = selenium.GetXpathCount("//div[#id='eventContent']");
but this brings only 1 as result and not two for this example.
when I try:
Console.WriteLine(selenium.GetText("//div[#id='eventContent'][1]"));
it prints all divs, but when I do:
Console.WriteLine(selenium.GetText("//div[#id='eventContent'][1]/div"));
it prints only the first div and I do not understand why.
Could someone be so kind and give me an explaination of whats going on here and where I'm wrong?
Thanks in advance
elur
decimal count = selenium.GetXpathCount("//div[#id='eventContent']");
This will return the count of divs that have an id of eventContent - there is only one div like this, which is why you get a count of 1 (count variables are typically ints rather than decimals, incidentally).
If you want the count of the contained divs, use
int count = selenium.GetXpathCount("//div[#id='eventContent']/div");
This will count the number of div children of the div with an id of eventContent. This should return 2, as desired.
As for your GetText examples, I think GetText will only return the text of the first node that the xpath argument selects. So with
selenium.GetText("//div[#id='eventContent'][1]")
you get the entire text of the parent div, which naturally contains all the child divs, but with
selenium.GetText("//div[#id='eventContent'][1]/div")
you get the text of only the first child div. This xpath selects all the child divs, but GetText operates on a single element only, I believe. If you want to examine the text of each child div in turn, you'll need to first get a count of the child divs, then use for loop to get each one in turn:
for(int i = 1; i <= count; ++i)
{
string childXpath = "//div[#id='eventContent']/div[" + i + "]";
string eventText = selenium.GetText(childXpath);
// Processing of eventText
}
A for loop and manual xpath processing are needed here (rather than the neater foreach), as I believe Selenium doesn't have a way of taking an xpath and returning a collection of elements.
tryed this but returns with 0. I solved this with a while expression where I check with isElementPresent like this:
int a = 1;
while (selenium.IsElementPresent("//div[#id='eventContent'][1]/div[" + a + "]"))
{
// check data here
a++;
}
seems to work so. thanks a lot for your help,
best regards elur