handling submenu item with webdriver selenium - java

I want to click submenu item using selenium webdriver which is invisible bydefault.
It becomes visible on mousehover .
I tried with some code and it is giving error as shown below
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with.
Here the code:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
//WebElement menuHoverLink = driver.findElement(By.className("current"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']"));
actions.moveToElement(subLink);
actions.click();
actions.perform();

Use the Actions class to do a mousehover on your menu item and then a click on the submenu option. You can refer to Actions class to get an overview of the methods available and a good help here to understand how to use these interactions.
Actions actions = new Actions(driver); WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']")).click();
I am hoping your locatros are correct..you might want to use a[contains(#href,'nemc.com/rentals')'

In some applications the Action interactions may not work. Personally i faced the problem and then i used the below solution. I took this solution from selenium issue tracker page.
WebElement targetElement = driver.findElement(By.id("locator"));
JavascriptExecutor js = (JavascriptExecutor) driver;
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
js.executeScript(mouseOverScript, targetElement);
driver.findElement(By.id("Click locator")).click;

Try using the below code. It should work.Try adding perform() to your moveToElement statement as shown below.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
actions.moveToElement(menuHoverLink).perform();
WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']"));
sublink.click();

I stumbled across a similar issue recently, with phantomJS and ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).
You can check the window size with
driver.manage().window().getSize()
And you can change it with
driver.manage().window().setSize(new Dimension(width, height));

Related

Close window button clicked but window is not closed

In selenium I successfully switch to an iFrame which contains a modal window:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='intercom-tour-frame']")))
In this iFrame is a close window button which is clicked "successfully" but the window does not close. By successfully I mean the button is found using the xpath and the action is completed without error in my code.
This is what I'm trying:
#FindBy(xpath = ("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))
private WebElement closeTestTourButton;
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
closeTestTourButton.click();
}
I've also tried:
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
waitForElementAndClick(closeTestTourButton, driver);
return this;
}
The test continues but fails as it tries to do an action but this is not possible due to the still open modal window.
Try clicking the button using the javascript, sometimes the events might not trigger with normal click.
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", closeTestTourButton);
return this;
}
I would suggest using the WebDriverWait rather delay in your script. Below is the implementation.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
Possibly you are switching and attempting to click() too early.
To click() on the close window button as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#name='intercom-tour-frame']")));
Induce WebDriverWait for the desired element to be clickable.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))).click();
But as you are using #FindBy presumably you are using PageFactory in PageObjectModel, so you won't be able to invoke WebDriverWait in conjunction with ExpectedConditions directly and you have to create a method. You can find a relevant detailed discussion in How to wait for invisibility of an element through PageFactory using Selenium and Java
Outro
Here you can find a relevant discussion on Ways to deal with #document under iframe
I don't like to answer my own questions but in this case this was the only solution that worked:
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
builder.sendKeys(Keys.ENTER).perform();
Granted, this is not the most elegant solution but after two days of trying it was the only one that worked.

How can I see the mouse pointer as it performs actions in Selenium?

I'm using Selenium Java WebDriver to automate UI tests. It works fine but it doesn't show the mouse pointer as it performs actions like clicking on a button. How can I make the mouse pointer visible as it moves on the page and clicks on a button?
You can highlight the elements that you are interacting with usings JS.
String jsSyyle = "'3px solid red'";
WebElement element;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.border=" + jsSyyle, element);
The way to do that is with Actions see documentation.
For example:
Actions action = new Actions(webdriver);
WebElement myElement = webdriver.findElement(By.xpath("the/xpath/to/element"));
action.moveToElement(myElement).click().build().perform();
Hope this helps you!

Clicking on an element in Selenium to bring up File Upload window doesn't work

In facebook navigate to Home and try to click on Add Photos/Videos to bring up the file upload window but it always gives an exception "Element is not clickable at point ..."
I tried the methods given in the first answer of Debugging "Element is not clickable at point" error
but nothing works.
Edit:
Code:
1.
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.click();
2
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.sendKeys(Keys.RETURN);
3
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
4.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element);
You can use Actions moveToElement
Actions actions = new Actions(driver); // initialize Actions instance
actions.moveToElement(elementToClick).performe(); // scroll the screen to make the button visible
elementToClick.click();
I just tried this and it worked... the File | Open dialog opens.
driver.findElement(By.cssSelector("div._3jk")).click();
Try this:
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);

Selenium - Java - Unable to click a link

My problem mainly is that my code doesn't run, I tried for more than 2 hours. I have seen many posts also, but some are written in different computer languages (not in Java), so I am confused now.
Below is my code for just clicking a button. All I want to do is click a button and go to new page.
WebDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/Users/Sanya/Desktop/New%20folder%20(2)/page%203%20alerts.htm");
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Try this it works fine for me:
WebElement menuHoverLink = driver.findElement(By.id("your_id"));
actions.moveToElement(menuHoverLink).perform();
You can try the below one...
Actions action = new Actions(driver);
action.click(driver.findElement(By.partialLinkText("Alert"))).build().perform();
It was worked for me :-)
You can use XPath for instance to locate the element on your page:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
Here is more information about how XPath works.

How to click on Hidden link through Selenium Webdriver

I am unable to click on a hidden link through Selenium Webdriver.
I am using the below code:
WebElement dwnld = driver.findElement((By.xpath("////form[#id='aspnetForm']/div[6]/div[2]/div/table/tbody/tr[3]/td[2]/table/tbody/tr[2]/td/a")));
Actions builder = new Actions(driver);
Action hoverAction = builder.click(dwnld).build();
hoverAction.perform();
If you don't want to show the link before clicking on it for some reason you can use javascript to click on it (see JavaScriptExecutor).
String Block1 = driver.findElement(By.id("element ID"));
JavascriptExecutor js1=(JavascriptExecutor)driver;
js1.executeScript("$("+Block1+").css({'display':'block'});");

Categories

Resources