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.
Related
Say I have an element (doesn't really matter what. Let's say "//div[#id='tony']/div/span" but any xpath would be OK.
To get the text on a page from this you could do something like
WebElement ele = driver.findElement(By.xpath("//div[#id='tony']/div/span"));
System.out.println(ele.getText());
// Or an our system someone wrote a method so we can get all text in that
// element and everything underneath (part of that element)
System.out.println(getText(ele));
This is fine when you are running the program. But originally when you are writing and debugging with Chrome before you actually write the program. In Chrome you can do right click -> inspect and then in the bottom do a ctrl-f which opens a search box and you can type an xpath and it will highlight it (them) on the page.
Now I swear several months ago when I was debugging I could also have it display the text in that element but I don't remember how? You type something into the search box like get-text(//div[#id='tony']/div/span) but I can't remember or figure it out. Perhaps at the time I was using Firebug and not Chrome?
Does anyone know a way to have it print the text of the element? I know you can eyeball it in the HTML code but what you think is the text and what it actually is is not always the same
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 :)
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);
Well...I think I still haven't figured out how xpath works. I am trying to select the below element so that I can click on the second page. But what I am trying does not work. What is wrong here? I am selecting the a tag and then trying to find the one which contains ref whose value is next.
element = driver.findElement(By.xpath("//a[contains(#ref,'next')]"));
I probably see a typo there. The attribute you are trying to use is rel which has value next
So the correct xpath will be
//a[contains(#rel,'next')]
I am searching for an element on a webpage by using its XPath. The XPath is correct and can find the element ONLY if I manually perform a click anywhere on the page first. If I do not do this before the statement runs it will not find the element.
I have tried many different attempts to allow focus on the page:
selenium.selectWindow(null);
selenium.fireEvent(xpath, "focus");
selenium.click(element on page);
selenium.mouseDown();
selenium.mouseUp();
After performing these functions the statement will still not find the element, but if I manually maximize the page and click anywhere on the page, then run the statement, the element is found. Can anyone help me out with this?
The statement I am using to find the element is:
selenium.isElementPresent("//div[#class='sample_class']");
Thanks!
Instead of using XPath, try using the CSS selector for that item.
Use selenium.isElementPresent("css=div.sample_class"); as your selector. Saucelabs have explained it quite well at http://saucelabs.com/blog/index.php/2009/10/selenium-tip-of-the-week-start-improving-your-locators/