Selenium webdriver click() fails in jenkins - java

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.

Related

JAVA Selenium chrome driver wont scroll while Page still loading

hoping someone can help me here, I have a website that takes a while to load, in order to successfully load all images on the page, I need to keep scrolling down while the page is still loading. I can not wait for the page to load first and then scroll.
I am aware of using the following to scroll to the bottom of the page, I have tried running this on a separate thread in a loop prior to and after getting the url with driver.get("SITE HERE"); to try and keep the page scrolling to the bottom until it has fully loaded.
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
This has not worked and I am at a loss on how to get selenium to scroll a page while it is still in the process of loading.
The webdriver get(String url) command states in its documentation that it will block until the page is loaded. If you call that you won't be able to try to scroll. I haven't ever tried it myself but you could try to load the page via JavaScript instead but it's not a great practice as you could very well end up with issues regarding the script context if you tried to loop in the same script call and could have timing issues if in a different script call (from the web driver). My first recommendation would be to talk to the page developer and make it so that scrolling after page load works correctly

Selenium Automation on IE11 (Enterprise mode)

I have installed Enterprise mode on IE11 to support applications which are only run on lower version of IE.
Using Enterprise mode on, to support application adhered to IE10 only I started scripting.
I am successfully able to traverse few pages but at one particular sidebar I am not able to click element.
It gets highlighted using javascript which means I find the element. But I am not able to click the element. it gets dotted square around the element.
I don't get any exception on that line.
There are no frames issue.
P.S: One of the post stated that there might be an issue with unexceptional behaviour of Tomcat. Due to which I find element but click() event does not work.
Try:
element.sendKeys(Keys.ENTER);
If not working try:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Selenium WebDriver does not find element by XPATH

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=pngNewC‌​ase1143241142570_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);

Using pre opened window with Selenium

I am trying to get Selenium WebDriver to find and use a browser window that is already open before the script executes. I am writting in Java.
I am using selenium-server 2.37.0 and the browser is IE8. I am open to using the Chrome browser as well.
Anyway, I have tried opening a driver instance and then looking for the window handles in the usual way (Set handles = driver.getWindowHandles();) but this only finds the hadle of the window that the driver opened. I have also just tried to switchTo the window by the window cannot be found.
The reason I want to use a pre-opened window is because when I execute my script, for some reason the browser won't let it click a link (It may be because the link is to an https address and sends a username and a token). When the script finishes the webpage won't respond to me manually clicking the link either.
I am able to manualy navigate to the link and click it the link works fine, so my thinking is that I can navigate to the page that I want and then kick of the scripts from there, but I need the webdriver to use this browser window that I used.
I cannot navigate the to link directly with Selenium because the link resided behind a secure server. I have to log in first then click the link and this is where I am having the problem.
Unfortunately, as of 2.37.1 (December 2013), it cannot be done.
There is an official feature request for this in the Selenium project (and it's even the most starred one), but it has not been done yet.
You can identify a browser window by windowHandle and switch between several Windows while testing.
You can print out all existing windowHandles (in your case it might be one) and then access it.
This solution worked for me (using Selenium 3.4.0):
Object[] handles = driver.getWindowHandles().toArray();
String windowHandle = handles[0]+"";
driver.switchTo().window(windowHandle);
After switching to your browser window you should be able to continue your test.
It relys on correctness of your webdriver- if you opened an Edge window the driver you use should be an EdgeDriver.

WebDriver unable to locate element (link/Java)

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)

Categories