Finding visible elements on webapp using selenium - java

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

Related

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

Unable to click on a button which is auto-focused by a script

I'm trying to click a button which is auto-focused by a script in the html.
I've tried both absolute path, relative path but no luck. pretty much anything I can think of. Attached is a screenshot which will display that the xpath is locating the element successfully on the web but when I use it in selenium, it can't find the element.
I've also tried waiting explicitly for 10 seconds for the element using a generic function. (Check the screenshot) This function is working for all other buttons that were called previously except for this one.
Something CAUGHT my eyes is that There's a method being called which auto-focuses the button. I might have to turn the focus to the entire window or the page in this case. I've also tried sending the enter key but still no luck.
Question_1: Does anyone have any solution?
Question_2: Does anyone know how to switch focus to the page?
Question_3: Or anything.
Any suggestion is highly appreciated.
Use Java Script click instead of click. Like this, Hope this will help.
WebElement element = driver.findElement(By.id("SubmitButtonId"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To switch focus to parent window/default content window you should use switchTo() method on driver instance.
driver.switchTo().parentFrame();
OR
driver.switchTo().defaultContent();
You can use following two statements to know which element is currently focused.
driver.switchTo().activeElement();
driver.getPageSource();
If you try to click on input tag , your script will fail. try to find correct locator only. it will help or try submit() function as you said, this element is auto selected.
I thinks you need to bring element in to view before clicking on the same:
the code for that is:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", webElement);

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.

Selenium WebDriver - Facebook Posting Issue

I'm really having trouble automating the facebook posting process.
My script is simply unable to track the textarea in our news feed correctly.
I've tried different selectors, even the ones suggested by Selenium IDE but got no avail.
Here's a snippet of my code:
//xpath for status update box
WebElement statusUpdateBox = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click the status update box
driver.findElement((By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN))).click();
//input data in the status update box
statusUpdateBox.sendKeys(DemoTestData.DATA_STATUSUPDATE_MSG);
WebElement postButton = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click 'post' button
postButton.click();
The xPath for
DemoTestData.XPATH_STATUSUPDATE_BTN = "html/body/div[1]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div/div[2]/div/div/div/form/div[1]/div/div[2]/textarea";
What am possibly missing?
Thanks a lot. :D
Here is what I've found. When I checked your xpath in my FB page it returned no results. (a very handy tool is a plugin called free simple firepath in firefox, be sure to get it if you don't!)
It seems unlikely that the button has an xpath ending with "textarea" are you sure that is correct?
When you run your test does the text get filled in? If not, the (first) problem is probably the findElement of the textbox.
When I looked some more I found that the css is actually very simple "textarea" . So if you'd use driver.FindElement(By.CssSelector(“textarea”))
that should return the text area.
The button has a bit of a strange css (to me, but I am quite new to this as well), but I beleieve you could find that by the text in it. In my language it says "Plaatsen" It might say something different for you. Check in Firepath!
The something like driver.FindElement(By.XPath("//*[text()[contains(.,'Plaatsen')]]")) should return the button.
I hope it helps! And I hope you found this answer useful, any tips on that would appreciated, it my first time answering something on the mighty Stack Overflow!
Cheers!

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