org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Cannot click on element - selenium-webdriver

Getting below exception error while running the code in IE browser. However same works fine Firefox and Chrome browser. Can someone help?
I am trying to run the below code in IE browser and trying to click on link "create button" on page https://en.wikipedia.org/wiki/Selenium_%28software%29. But link never gets clicked , same code works fine in Firefox.
can you please advise if I need to make any changes
public class ClickIE {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("requireWindowFocus", true);
System.setProperty("webdriver.ie.driver", "C:\\Users\\Nitin\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://en.wikipedia.org/wiki/Selenium_%28software%29");]
Thread.sleep(5000);
driver.manage().window().maximize();
driver.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/ul/li[4]/a")).click();
String url = driver.getCurrentUrl();
if(url.contains("wikipedia")) {
System.out.println(url+" its a internal link - Passed");
}
else {
System.out.println(url+" its a external link - Failed");
}

Related

Page does not open in the new tab when clicking on element using Selenium WebDriver and window handler [duplicate]

This question already has answers here:
Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium
(1 answer)
Selenium Web Driver in IE - Control is passing to childwindow from parent window but unable to locate the elements
(1 answer)
Closed 4 years ago.
I'm new to Selenium and trying to work with multiple windows.
I'm able to open the initial page and display its title in console.
The title of the main page will print.
Then, I locate the element to click on.
When clicking on it, the page is supposed to load in the new tab and I need to print out new title of the page.
However, nothing happens. Only new tab is opened and nothing happens afterwords.
This is the code:
public class WindowHandlerPractice {
static ChromeOptions options;
static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\eugeneg\\local-eclipse-workspace\\webdrivers\\chromedriver\\chromedriver.exe");
options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
driver.get("https://www.msn.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//div[#class='mestripescrollfix']//ul[#role='menubar']//li[2]")).click();
//driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Set<String> windHand = driver.getWindowHandles();
Iterator<String> it = windHand.iterator();
String parentid = it.next();
String childid = it.next();
driver.switchTo().window(childid);
System.out.println(driver.getTitle());
}
}
Where is my mistake?
I just tried the above scenario and it worked for me.
Could you try with the below changes
Avoid setting chromeoptions
Use latest chrome and chromedriver

Selenium Web driver- How to Handle the Dynamics Table and Click Specific Element

I'm new in Selenium webdriver and learning the Dynamics table as the moment im stuck at point. i want to click particular company name in dynamics table i have written sample scripts for it please let me whats wrong with it.
im using icicidirect website.
Selecting the Market link from main menu bar
Now at the bottom of the page their is one link "Daily Share Prices" link (its below the "Top Losers" section will get it by using ctrl+f)
At Daily Share Prices in first column (Security Name) i.e.ABB link element is their
and i want to click that element
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.icicidirect.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//a[contains(text(),'Markets')]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[contains(text(),'Daily Share Prices')]")).click();
Thread.sleep(3000);
TablePageObject tablePageObject = PageFactory.initElements(driver, TablePageObject.class);
tablePageObject.clickLink("ABB");
}
}
public class TablePageObject {
private WebDriver driver;
#
FindBy(css = "table tr")
private List < WebElement > allTableRows; // find all the rows of the table
public TablePageObject(WebDriver driver) {
this.driver = driver;
}
public void clickLink(String SecurityName) {
for (WebElement row: allTableRows) {
List < WebElement > links = row.findElements(By.linkText("ABB"));
// the first link by row is the company name, the second is link to be clicked
if (links.get(0).getText().contains(SecurityName)) {
links.get(0).click();
}
}
}
}
Several suggestions:
You may use the following link in order to receive the required table directly
driver.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices");
You may wait for the table loading
Then you will found the element and click it (like in your code).
This is code that works for me
WebDriver driver = new FirefoxDriver();
try{
driver.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices");
(new WebDriverWait(driver, 10/*sec*/)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("ABB")));
List<WebElement> dailyList = driver.findElements(By.linkText("ABB"));
if (dailyList.size()!=0) {
dailyList.get(0).click();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
driver.close();
}
If you need find some element that not located on the first page you may extend this solution to click on the Next>> link and back to this loop improving it by removing hard coded "ABB" element.

How can I run Fluentlenium Code inside Selenium Webdriver Firefox Driver?

I am having issues trying to get my Fluentlenium code to run inside the WebDriver Firefox Driver. I need Fluentlenium to execute inside the WebDriver Firefox Driver instead of opening it's own browser. I think I need to override this but I am not exactly sure how to do this. Any help would greatly appreciated. Thanks! Here is what I have for code:
WebDriver driver = new FirefoxDriver();
#Test
public void create_a_picklist()
{
// Go to Page
goTo("http://www.google.com");
}
What happens is that it opens two browsers. One is from the Firefox Driver and the other must be the default browser from the goTo from Fluentlenium. I need it to run this code inside the Firefox Driver window and not open it's own window from Fluentlenium.
By default, it launchs a Firefox browser so that's sufficient :
public class Test extends FluentTest {
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}
And nothing more :)
Ok. Looks like I figured it out. Here is what I did to override the browser:
public class Test extends FluentTest {
// Defines the Driver
public WebDriver driver = new FirefoxDriver();
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
return driver;
}
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}

Alert doesn't close using Selenium WebDriver with Google Chrome.

I have the following Selenium script for opening alert on rediff.com:
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
Thread.sleep(5000);
Alert alert=driver.switchTo().alert();
alert.accept();
}
}
This very same script is working fine in Firefox and IE9, however using Google Chrome after opening the alert, rest of the code is not working. The main thing is that does not shows any exception, error or anything.
Please provide any solution as soon as possible.
Thanks a lot!
Note: If we need to change any setting of browser or any thing please let me know.
Selenium version:Selenium(2) Webdriver
OS:Windows 7
Browser:Chrome
Browser version:26.0.1410.64 m
I'm pretty sure your problem is a very common one, that's why i never advise using Thread.sleep(), since it does not guarantee the code will run only when the Alert shows up, also it may add up time to your tests even when the alert is shown.
The code below should wait only until some alert is display on the page, and i'd advise you using this one Firefox and IE9 aswell.
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
}
Mostly all that is done here, is changing Thread.sleep(), for a condition that actually will only move forward on the code as soon a alert() is present in the page. As soon as someone does, it wil switch to it and accept.
You can find the Javadoc for the whole ExpectedConditions class here.
Unfortunately AlertIsPresent doesn't exist in C# API
http://selenium.googlecode.com/git/docs/api/dotnet/index.html
You can use something like this:
private static bool TryToAcceptAlert(this IWebDriver driver)
{
try
{
var alert = driver.SwitchTo().Alert();
alert.Accept();
return true;
}
catch (Exception)
{
return false;
}
}
public static void AcceptAlert(this IWebDriver driver, int timeOutInSeconds = ElementTimeout)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOutInSeconds)).Until(
delegate { return driver.TryToAcceptAlert(); }
);
}

Different Screen Shot Resolution in Different Browsers

I am facing a problem related to my Project of GUI comparison...
It takes Screen Shots of given URL in different Browsers, but these Screen Shots are having different Resolution for different Browser.
So, my problem is that now what to do for getting the Same Resolution of all the screen shots in different Browsers.???
If any solution is there then kindly tell me.
Detail:
Resolutions With:
Mozilla Firefox:- 1345*627
Google Chrome:- 1345*659
Internet Explorer:- 1345*679
Tools used:
Selenium Web Driver.
Java
Try maximising the driver window
Example with Junit & webdriver:
public class Untitled {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}`

Resources