Multiple "conditions" for CSS wildcard selectors in Selenium - java

I have multiple CSS webelements whose ids have the following form:
cell_[number]-button_[number]
I want to select ALL such webelements by asking Selenium to find all elements that start with cell and contain button:
var elements = Util.driver.findElements(By.cssSelector("[id^='cell_'] and [id*='button']"));
Is this the correct way to do it? Running this code returns a list with zero elements, so something is wrong.

This worked for me:
"[id^='cell_'][id*='button']".

Related

How to find the menu bar has x number of tabs visible on the page

I am using the site https://www.shoppersstop.com/ to practice Selenium web driver using Java.
I wanted to find that the menu bar has x number of tabs or all the tabs visible on the page. I can write method for each of the tab that is displayed on the page but I am unable to write a single method that finds all the elements visible on the menu.
Selenium does support finding elements by xpath. To keep things simple, I would suggest get all elements matching the xpath you need and then filter only those that are needed.
If using Java 8, this should work:
driver.findElements(By.xpath("//ul[#class='lvl1']/li"))
.filter(element -> element.isDisplayed())
.collect(Collectors.toList());
xPath Explanation: First, get me all 'ul' elements that have the class 'lvl1'; then get all 'li' elements that are found immediately inside these 'ul' elements.
There are of course other ways that you can use, especially if you owned the website. For example, if you knew that any visible element will have the class 'show-item', then you can compose your xPath as: "//ul[#class='lvl1']/li[#class='show-item']".

How to find xpath element partially using selenium with java?

I'm trying to select partially an element with xpath in my selenium code.
My xpath is
//iron-pages[#id='pages']//span[.='s8718216']
what I want is to select any element starting with s, the element just after span.
I tried this:
//iron-pages[starts-with(span[.='s'])
It doesn't work for me.
Can someone help me.
I think this xpath should work //iron-pages[starts-with(text(),'s')]
Or second try:
//iron-pages[starts-with(.,'s')] <- . instead of text() checks element for more properties. Not only text.
There are many properties that might contain text like innerText, innerHTML etc.
EDIT:
I just read your question again. You want to select element right after span so:
//iron-pages[#id='pages']//span[starts-with(text(),'s')] <- it will select span elements starting with text s.
If you want child elements you can use
//iron-pages[#id='pages']//span//*[starts-with(text(),'s')]
Your xpath should be
//iron-pages[starts-with(span[.='s'])//following-sibling::i[1]
it will get the next element that start with span with text s

Selenium: Counting Total <th> Using XPATH in Java

I am trying to count the total using the xpath. The xpath is very long and it goes like this:
String path = "/html/body/div/div[3]/div/div[2]/div[2]/div[1]/table/tbody/tr/td/table/tbody/tr[5]/td/div/table[4]/tbody/tr/td/table/tbody/tr/td[3]/div/div/div/div[1]/div[1]/table/tbody/tr[1]/th";
This is my code:
List <WebElement> pagination= driver.findElements(By.xpath(path));
page_size = pagination.size();
The result of this is incorrect. I'm expecting 37 because there are 37 th's but it's only giving 31. Any idea what's wrong with my code? Thank you!
First, make sure your XPath is correct. You can do it in Chrome developer tools, that is opened by F12. Click RMB on the element you want to find and select Inspect element. There you'll see the Elements tab opened with your element highlighted. Click RMB on it, then Copy > Copy XPATH.
You can test this XPATH in search window in chrome's DevTools or (as I prefer) in FirePath for Firefox. If you make it less specific, it will show you all the elements, that are suitable for your XPATH.
You can try fetching parent table element with some other selector like css selector,id,name or whatever is unique for that table.
Once you have that element you can use
driver.findElements(By.tagName("th"));
This should return you all the 'th's from required table.
It will be very helpful if you could share the html page source of the page.

Get the child of an HTML element by its XPath

I have a really simple ask, i have a div in a html page that i can acces by his XPath which is : //div[2]/div/div[2]/div[2]/div[2].
I want a XPath that would give all the child of this div but i can't find it.
I need to get the element with the findElements method of Selenium, but what i've tested does not work:
My HTML code looks like this :
<div>
<input/>
<span/>
<p></p>
</div>
And the XPath I want to use like this :
//div[2]/div/div[2]/div[2]/div[2]/child
And my Java Selenium script like this :
List<WebElement> listElement = driver.findElements(By.xpath(xpath));
for(WebElement element : listElement) {
System.out.println(element.getAttribute("id"));
}
What XPath should I put to get the child of the div?
EDIT 1: I did use the * and it's working, but when i count the number of element it prints me 6. Does the * consider the children of his children as his own children ??
If the div in the HTML fragment in your question is located at
//div[2]/div/div[2]/div[2]/div[2]
then the input child element would be here:
//div[2]/div/div[2]/div[2]/div[2]/input
and all of the children elements would be here:
//div[2]/div/div[2]/div[2]/div[2]/*
Update:
EDIT 1: I did use the * and it's working, but when i count the number
of element it prints me 6. Does the * consider the children of his
children as his own children ??
No, div/* selects only the immediate children elements of the parent div.
If you're being surprised by a greater number of children than expected, it may be that the base XPath is selecting multiple elements, and you're then selecting the children of more than just the targeted div element.
Update 2:
If you cannot post a MCVE, and you're still puzzled about the number of children returned by,
//div[2]/div/div[2]/div[2]/div[2]/*
try challenging yourself on the XPath you've provided as the base:
//div[2]/div/div[2]/div[2]/div[2]/
First, try
//div[2]
Does it really select the single div as you expect?
Then try
//div[2]/div
Again, see if this one really selects the single div you expect.
Continue in this manner until you get to a place where the reality of the selected elements deviates from your expectations. There your answer will be obvious, or your have a more specific question to ask.
Without seeing your XML/HTML, this is about as good as the advice can get.
List<WebElement> childs = driver.findElements(By.xpath("//div[2]/div/div[2]/div[2]/div[2]/*"));
In that case selenium searchs for all child elements from path div[2]/div/div[2]/div[2]/div[2]/ and set them in list collection as WebElement object.

Handle a table in Selenium Webdriver

I am trying to automate a Test Case, but in one of the steps I have a table and I can´t handle it good.
Looking at the code with the development tools, I see a very large list where all the elements of the table are stored. In this link you can see an image of a small part of the code.
http://www.m-i-u.de/display-i94067b1tygv.html
In a certain row of the table is the element "Deadlocked" and I have to check whether in the 2 following rows there are 2 "Nein" (In this case both "Nine" are there)
The thing is that I dont have any ID and I don´t know how to locate this 3 words (Deadocked, Nein, Nein) in the code. Does anybody have any idea I could try?
I would really appreciate any help
Thanks a lot
Pablo
Firefox webDriver element locator plugin is very easy tool to locate elements in UI just by right clicking on elements. You will be able to get the set of selenium locators to identify elements mentioned.
https://addons.mozilla.org/en-us/firefox/addon/element-locator-for-webdriv/
Find all the elements using className attribute. An example:
List<WebElement> links = driver.findElements(By.className("c"));
links.get(0); //this will give Deadlocked
links.get(1); //this will give Nein
links.get(2); //this will give Nein
Using XPath:
List<WebElement> links = driver.findElements(By.xpath("//tr[#class=\"e\"]/th"));
links.get(0);
links.get(1);
links.get(2);
Using cssSelector:
List<WebElement> links = driver.findElements(By.cssSelector("tr[#class=e] > th"));
links.get(0);
links.get(1);
links.get(2);

Categories