I'm trying to write a Selenium Testcase and started with the Selenium IDE in Firefox.
The test is working quite fine there. Now I want to automate that test with Selenium Webdriver and exported the according JAVA class.
Everything is setup and working fine so far (the Internet Explorer window is opening, the according page is displayed).
BUT: Selenium Webdriver does not find the element.
I want to get the following element:
<div class="x-grid3-cell-inner x-grid3-col-instance/status" unselectable="on">Open</div>
and I have the following code:
WebElement openWorkItem = driver.findElement(By.xpath("//div[contains(text(),'Open')]"));
System.out.println(openWorkItem.getText());
In the testcase with Selenium IDE the statement is pretty much the same, and it is working:
<td>//div[contains(text(),'Open')]</td>
The only difference is, that the Selenium IDE is only available in Firefox, but in the end I want to have the test executed in IE with Selenium WebDriver.
Any help is very much appreciated.
==Update==
I executed driver.getPageSource() and now see that the HTML part I am looking for is not available because it seems to be located in an iFrame. Any ideas?
You can use findElement to get the frame webelement and have it used with the switchto() method.
driver.switchTo().frame(driver.findElement(By.xpath("iframe[contains(#name=pngNewCase1143241142570_IFrame)]")));
If you have some other attribute like src, you can try the below.
WebElement frame=driver.findElement(By.xpath("//iframe[#src='showNewClaimForm.action']");
driver.switchTo().frame(frame);
Related
I'm trying to use Selenium to test a Firefox add-on, but it doesn't find the "Preferences" button in about:addons:
extensionEntry.findElement(By.className("preferences")).click();
results in
org.openqa.selenium.NoSuchElementException: Unable to locate element: .preferences
Is this the wrong way to get a reference to the button? I've tried navigating the XUL in the Inspector, and there it definitely has the right class.
Selenium is not what you should be using to write test scripts for these type of applications.
As suggested by AutomatedTester, refer to his answer https://stackoverflow.com/a/5700516/3465845
I have written a script for datepicker to choose dates from the calendar. The scripts are running fine in local, but when I run it through jenkins the script is getting failed.
action.moveToElement(driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span")));//locating the element to click
action.perform();
action.click(driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span"))); //this line is not executing
action.perform();
The script to click the element is not working. I am getting error as "Element is not currently visible and so may not be interacted with"
I have also tried driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span")).click() by replacing action.click() but still no use.
I have faced a similar issue and after a couple of frustrating hours, I have figured out, that im my case only one thing has worked for me - JavascriptExecutor.
I don't know why all other attempts have failed(all of them have worked well locally). It seems like Jenkins specific issues.
Anyway you can use this code snippet:
WebElement elem = driver.findElement(By.xpath("//path/to/element"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", elem);
Note: in my case I was always able to send click action to element, but somehow browser didn't react on this action. So element was remained unclicked without any error.
You cannot click on the hidden element using selenium because if will throw the exception that you saw. You should either make element visible (in the way the user does so) or use javascript to click (see JavaScript executor).
Clicking on an element works fine locally but not in Jenkins;
first, I was locating the web element using XPATH, and when it came to clicking I tried selenium click, actions click, js click. All were working locally But not in Jenkins.
Finally what worked for me was the combination of css selector with javascriptExecutor click. This solved my problem. Now works both locally and in Jenkins.
So try the same.
I am doing an automation with Selenium Webdriver using Java. I need to click the class name inside the span tag.
Example
For this i used xpath to select class="selcteME" and used click(). This works fine if I put break point in debug mode but when I run the script without break point in debug mode object is not selected.
Please share if you have solution or suggest me how can I get the class name inside the span tag
() without xpath
Have you tried by CSS Selector?
I suggest use some IDE to generate basic test and then use your logic.
I'm using Selenium to navigate a webpage which has a link called "Mail", using WebDriver (just recently switched from RC to WebDriver). I want to click on the link but the testcase always fails with the error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"link text","selector":"Mail"}
When inspecting the element with Firebug I get the following HTML:
Mail
This is the Java which attempts to click the link:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.linkText("Mail"));
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
I can see that the element is present on screen but still, the test case fails.
Does anyone know what I might be missing here or an alternative way to find the link element?
Try via XPath. Example:
driver.findElement(By.xPath("/a[text()='Mail']"));
Would also be worthwhile double checking to ensure there are no iframes on the page.
Even i had faced this situation once. View your source code and find out if the element you are looking for is inside a frame. If yes, first switch to the frame in which the element is located and then look out for the element. It worked this way for me.
So far the best workaround I found for cases like this:
Open Firefox with Selenium IDE installed
In firefox open the test page
Run selenium IDE, start recording and click the link
Selenium IDE will offer you ways how to locate the target. If you switch to relative XPath, it should do the magic (Or, in my cases, it always helped)
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.