Handle a table in Selenium Webdriver - java

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);

Related

Multiple "conditions" for CSS wildcard selectors in Selenium

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']".

Can't inspect search button in IntelliJ-selenium Chrome driver

I am working on automation testing website www.benjerry.com/.
I can't inspect serch button.
I succesfully enter credential in search field, but can't click on search button.
I try all kind of combinations, but still nothing.
Can someone please help me?
Picture with marked problem
Code line:
driver.findElement(By.cssSelector("div.btn-submit")).click();
As you have already accepted the answer given from #kushal. I'd like to highlight some points that could be useful in your Automation Journey .
You can use this cssSelector : input[id='searchTerm'] ~button>span
your code would like this :
driver.findElement(By.cssSelector("input[id='searchTerm'] ~button>span")).click();
Here are some benefits of using this cssSelector :
cssSelector are more consistent than Xpath.
Xpath Provided by Kushal contains numeric that is [2], which is never gonna be consistent , your test may fail if Dev would put one more span inside Button Tag. So in case if you still want to go with Xpath , then your Xpath should look like this :
//input[#id='searchTerm']/following-sibling::button/span
You can find the difference between cssSelector and Xpath here :
What is the difference between css-selector & Xpath? which is better(according to performance & for cross browser testing)?
Try following line of code:
driver.findElement(By.xpath("(//button/span[contains(.,'Search')])[2]")).click();

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.

Java Selenium - How to interact with id-less/class-less element?

I'm trying to interact with a button on a page. Linktext and xpath do not work, there are no classes or combinations of selecting elements and looping through them I can find that work.
Here is the screen shot of the code I'm trying to do a .click()
Please help me how do i achieve the same ?
I think you have 2 options as below. I simplified your example HTML code to smoke test these queries:
Select an element based on its content. The drawback is of course that as soon as "Historical Scans" label changes to something else your query will stop working.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]//a[normalize-space(.)="Historical Scans"]
(working example on xpath tester http://xpather.com/dqZ7UWvz)
Select an element based on the position on the list. The downside is that it will stop working once this element changes its position.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]/li[3]/a
(http://xpather.com/rgexHKBB)
Based on my experience you should not rely on any other attributes or elements. Ideally, the best option would be to add ids/classes. Please let us know if this solves your problem.

Finding visible elements on webapp using selenium

I need help to find visible elements on a webapp page using selenium.
For example, I need to find a row that is click-able and click on it.
The problem is that the elements are visible but not present in the page source as they are generated. Also, I'm coding in Java to find the elements. I tried using xpath and css path which I found using Firefox Firebug, but it didn't work. The click-able rows in the table all have the same class. They have different text though. Any help? Thanks!
You can use isDisplayed() method for same
boolean aa = driver.findElement(By.xpath("YOUR Element")).isDisplayed();
boolean ab = driver.findElement(By.xpath("YOUR Element")).isEnabled();
boolean ac = driver.findElement(By.xpath("YOUR Element")).isSelected();
For more detail info please refer the below URL:-
http://www.softwaretestinghelp.com/webdriver-commands-selenium-tutorial-14/
Hope it will help you :)

Categories