How to use Xpath of button in Selenium RC? - java

I am working in Selenium RC. Can anyone please let me know how to write xpath for button in Selenium (Java)?

You should develop the script in the Selenium IDE (download) before porting it to Selenium RC. In Selenium IDE, when you click anything on the webpage, it should automatically generate some kind of selector for the element you clicked. Then, once you've recorded all the events, you Format it in whatever language you're using, and then you copy and paste it to your Selenium RC code.
But the Recorder Javascript isn't foolproof (e.g. if you click on a div that causes some XMLHttpRequest or setTimeout, it won't be recorded). Or, the click may be recorded but you may not like the selector that Selenium chooses for the element. In either case, you'll have to write your own selector based on the DOM structure. To see the DOM structure, open Firebug if you're in Firefox (F12), or open the Inspector if you're on Chrome (Ctrl-Shift-J) Fortunately, Selenium understands a bunch of selector syntaxes, so you can use CSS selectors if you don't know XPath.
If you do decide to use XPath, you'll have to learn it first. I haven't found any good tutorials (and I'm not a fan of w3schools). But feel free to use a bookmarklet to test XPaths that I wrote. You'll probably end up with something like //button[.="text on button"], or //input[#value="text on button"].

You can find button Xpath by using Firebug which is an add-on for Firefox and as above answer Selenium IDE is also another, easier option to find.

Related

I am trying to access secure website where right button click is disabled. by using selenium Xpath cannot click on elements

The website which for which I am trying to do automation is a secure website where some functions like right clicking have been disabled. I am using the exact xpath for the login input field but it is not working. My Xpath is as follows:
driver.findElement(By.xpath("//input[contains(#class,'input')]")).sendKeys("Aa12");
SeeTest provides a way for opening the developer tools even in such cases.
They also have different versions of chrome in which you can try your scenario.
Their documentation might help you more on this.

Jsoup: Getting a link that doesn't show in the HTML

I am working on a little app for myself. I am trying to get a list of links from a site. The site is for example: http://kinox.to/Stream/Prison_Break.html
If you hover over the big window in the middle that says kinox.to best online, it show the link that I want in the bottom left. The problem is if I look at the html file I can't find the link anywhere. I guess it has to do something with the site using JavaScript or Ajax.
Is it possible to somehow get the link using JSoup or are there any other Java libraries that could help me?
I did not look closely into the page you try to load, but here is what I think the problem may be: The link is loaded/generated dynamically via JavaScript. Jsoup does not run JavaScript, so therefore you can't find the link in the html.
Two possible solutions:
1) Use something like selenium webdriver to access the content. The Java bindings allow to remote control a real browser which should have no problems loading the page and running all scripts within. Solution 1 is simple to program, but runs slowly. It may depend on an extern browser program which must be installed on the machine. An alternative to webdriver is the JavaFx webkit engine in case you are on java 8.
2) Analyse the traffic and the JavaScript on the page and find out where the link comes from. This may take a bit of time to find out, but when you succeed you can use Jsoup to get all the data you need. This solution should run much faster than solution 1.
One solution and probably the easiest would be to use Selenium:
WebDriver driver = new FirefoxDriver();
driver.get("http://kinox.to/Stream/Prison_Break.html");
String mylink = driver.findElement(By.cssSelector("#AjaxStream > a")).getText();

Capture user input and actions with Selenium WebDriver using Java

Is it possible to capture user input/actions with Selenium WebDriver, in the same way that you can use the Selenium IDE for recording / creating tests?
i.e. when the user enters a URL, clicks a link, fills in a text box, clicks a button etc etc.
I'd like to be able to capture these actions using the WebDriver rather than just using the Selenium IDE, as I want to integrate with other classes available in my Java application.
I attempted to offer a viable solution in Record Actions using Selenium
Hope this helps.
You can't 'record' a set of actions with Selenium WebDriver, you will need to write those steps manually.
Strictly speaking you can capture user input by using the WebDriver API in your chosen language (C#, Java, PHP, Ruby. Python, Perl or JavaScript) and it vaguely resembles using the DOM. If it suits your requirements you could use configuration files to supply some of your user input.
Navigate to a URL:
WebDriver driver = new FirefoxDriver();
driver.get('url')
Click a link/button:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.click();
Enter Text in a field:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.sendKeys('userinput');
For more information on the API Selenium HQ is pretty definitive:
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
If you're going from Selenium IDE to writing tests it'd be really useful to check out the page object pattern as I've found it makes your tests more maintainable in the long-run. This link is a good starting point because it gives an overview, and a visual representation of what you get by following the pattern:
http://blog.josephwilk.net/cucumber/page-object-pattern.html
Hope that helps.
As far as I'm aware, there isn't an easy way to do it - but recording on IDE and exporting as a java file has worked well for me (File -> Export test case as...). I usually do it to c# but have used it with java.

How to do drag and drop from file system to browser using Selenium Web driver

I am trying to automate a requirement, where I have to drag images(jpeg or png) form a file and drop it to browser.
I am using Selenium Web Driver as automation tool with JAVA, I tried but selenium doesnt support anything outside browser.
I need help can anyone guide me how can I automate this.
You would not be able to do this using selenium. The selenium bindings have access to the browser instance (the WebDriver) but to nothing outside of it.
You better use autoit for it.
Drag and Drop
If your trying to drop your image into some sort of input you could try this. I used this to upload files with selenium.
WebElement inputField = driver.findElement(By.xpath("//*[#id='MyInputField']");
inputField.sendKeys("path to image");
Hope this helps.

Finding Elements in a Redrawn DOM

We have a website which has a wijmo grid in it. I am attempting to test this website. In IE the grid is drawn, yet when I go into the "Developer Tools", the grid is not actually there. This makes running Selenium tests very difficult as Selenium cannot find it either.
Has anyone run into this before? If so, how do I successfully run my tests?
The grid will be part of the DOM and you should be able to select it successfully with the correct path using Selenium.
I believe it is the tools that are misleading and are simply not updating when the DOM changes.
I would try checking another browser, for example Google Chrome (F12 in Windows). In my experience the IE Developer Tools are not as powerful or feature rich as the Chrome tools. The Chrome tools do update the DOM when elements are manipulated.
It looks like it is working now. The Developer had a pre-loader which was causing the problem. Once that was disabled it is now able to find the elements.

Categories