Selecting an option in a CSS-based menu with WebDriver - java

I have a simple CSS-based dropdown menu, and I'm trying to click on one of the menu items in a Java Selenium (WebDriver) test.
Both the menu (<ul> element) and the items (<a>) have IDs and creating corresponding WebElement objects works fine. I'm trying to click on one of the items with code like:
hoverOver(transfersMenu);
transferLink.click();
In hoverOver(), I've tried all three answers from this question, but none of them work. I keep getting:
org.openqa.selenium.ElementNotVisibleException:
Element is not currently visible and so may not be interacted with
Command duration or timeout: 2.06 seconds
(I've tried calling transferLink.click() also before hoverOver(), in the hope that the implicit wait would make it work, but nope.)
Any idea how to make the hovering work so that the link can be clicked?
Selenium version 2.21.0. I'm running the tests on Linux (Ubuntu), using Firefox 13.0. A colleague just tried on Windows (using Firefox 12.0), and it didn't work for him either.
Update: As per Slanec's tip in comments, and these instructions, I tried setEnableNativeEvents(true) on the FirefoxProfile. At first this failed:
org.openqa.selenium.InvalidElementStateException:
Cannot perform native interaction: Could not load native events component.
...but after I upgraded to Selenium 2.23.1, I no longer get that complaint.
Still, the hovering doesn't work (with native events on or off). :-/

I use the following code to hover over our menus for 1 second, before clicking a link, just like the one you are using:
action = new SeleniumActionHelper(driver);
WebElement currentUser = findElementByLinkText("testing1");
action.mouseHover(currentUser);
Thread.sleep(1000);
Of note, the mouse cursor needs to remain in the browser window for the hover to keep. If the mouse cursor is outside of the browser window, I experience a quick flash of the menu, but it does not stay visible

Try this exampale:
WebElement menuHoverLink= driver.findElement(By.id("test"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.id("test")).click();
Thread.sleep(6000);

How do you run your test classes? I found out that running WebDriver through ANT makes hover actions impossible, whereas running the test classes from command line (TestNG JAR) or from Eclipse works just fine.

Related

Selenium click chrome physical buttons like menu, left, right navigation, bookmarks

I'm working on a some automation work, as per my requirement I need to click on Chrome Physical buttons like left nav, right nav, bookmarks, menu etc. I can do with shortcuts but my requirement is to click on browser buttons. Any ideas would be helpful. Thanks in advance.
As per your question you want to click on Chrome Physical buttons like left navigation, right navigation, bookmarks, menu etc.
But if you look into the documentation in Selenium Home Page it clearly mentions that :
The entire suite of tools provided by Selenium results in a rich set of testing functions specifically geared to the needs of testing of web applications. These operations allow many options for locating UI elements and comparing expected test results against actual application behavior.
So factually Selenium by design interacts with the HTML DOM and the WebElements located in the DOM Tree
Now the desired controls e.g. left navigation, right navigation, bookmarks, menu are out of the DOM. Hence you cannot mock the click on those controls.
However all the Selenium Language Binding Art provides a handfull of methods to achieve the same result. Here are a few from the Selenium Python Binding Art :
Maximize : To maximize the browser window.
driver.maximize_window()
Minimize : To minimize the browser window.
driver.minimize_window()
Close : To close the browser window.
driver.close()
Quit : To close the browser window gracefully.
driver.quit()
Refresh : To refresh the url.
driver.refresh()
Forward : To move forward.
driver.forward()
Back : To move backwards.
driver.back()
And of-coarse Get : To invoke an url.
driver.get('http://google.com/')
There are functions for this that is built-in:
driver.forward()
driver.back()
https://selenium-python.readthedocs.io/navigating.html#navigation-history-and-location
It doesn't appear that selenium can interact with the Bookmark, but let me check some more.
This can't be done with selenium webdriver and I think also not with the standalone selenium server. Selenium only allows to interact with the DOM.
The only way to achieve what you want to do is to use an automation tool that actually runs directly in the OS that you use. Java can be used to write such a program.
I would however recommend to not go this route. Instead try to convince whoever is responsible for your requirements to re-think and allow to use other means of achieving back and forward actions.

Java Phantomjs behaving different in windows and linux

I been trying to solve this issue but not sure what's the cause for this,
i made a program with java and phantomjs, and in theory they both should behave the same way.
Phantomjs in both the pc and the server is the same v.2.0.
In windows(v.7) mi testing program works as expected, but in linux (debian) the program fails, with this error when it tries to click an element:
Caused by: org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with css selector
Now, i know sometimes we have to wait for few seconds for the page to load, which already do (even added a few more just to be sure) and it was tested correctly in windows.
i tried with the click function:
element.click();
with a javascript code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element)
and with actions too:
Actions action = new Actions(driver);
action.moveToElement(element).contextClick().build().perform();
//and this code too
action.moveToElement(element).click().build().perform();
Anyone had this same or similar issue? what i else i can do? i'm stuck since yesterday and i wan't able to figure out this problem.
note: for now i have an alternate way to get page without having interaction, but still having different results from the web page when executing in different OS, for example:
where i normally i get this link: www.somesite.com/?search=xxxxx&date_in=dd/MM/yyyy&params=etc. , instead i get www.somesite.com/?search=xxxxx, the default search without any especific search parameters.
You need to use onResourceError callback, to find out what's going wrong.
You also need to use the following Command-Line options:
--ignore-ssl-errors=true --ssl-protocol=any --debug=true
See also: this issue.

Error: When i minimize Internet Explorer which is running the automated WebDriver Code

I have written the Selenium webdriver java code to automate the test and its working fine. But I have lot of data input to test my web and it takes time. So when i minimize the IE to do some other task while it is running the automation, it is throwing error:
org.openqa.selenium.ElementNotVisibleException: Element is not displayed
Selenium WebDriver is trying to simulate "real" users interaction with the webpage. If a person can't click on a button not currently displayed, neither can Selenium.
ElementNotVisibleException occurs when the element you want to interact with is not displayed. When you minimize the browser some of the elements are no longer visible, even though they where in maximized window.
You can add scroll using moveToElement() from Actions class every time you want to perform any action (I don't recommend it, you increase significantly the chance for errors), or find another hardware solution, like plugging in another screen, run the test on another computer etc.
According to my experience, the Internet Explorer WebDriver is very oversensitive when it comes to disturbances from a real user while running test cases. It's better to not touch anything at all. ;-)
Try Chrome! This is much more robust and also faster.
Selenium script runs as a simulator. You cannot do another work when script is running. Chrome is fast but while running script in chrome you can not do other task like any other browser. If you minimize window, you will get exception "ElementNotVisible".

Unable to click on Submit button in Remote Machine where BitDefender is installed using Webdriver in Java. Please find the details below

Unable to click on Submit button in Remote Machine where BitDefender is installed using Webdriver in Java. The issue is:
I have to execute my test scripts by creating a jar file in Remote Machine, where BitDefender is installed in it. when executing the jar file in IE browser in Remote Machine, I am Unable to click on Submit button (find Screenshot1), because the BitDefender tool bar is coming on Submit button when running it using Webdriver. I am getting an Exception here.
Actually when executing it Manually, I can click on Submit button with out any issues, i can click on it as in Screenshot2
Please can any one help me out on this issue without closing or moving the BitDefender tool bar in IE browser. Help will be appreciated.
Using the Actions() class.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("submit")).moveByOffset(x,y).click().build().perform();
//where x and y are the visible lower right corner of the button - you can find its dimensions from firebug or similar
Finally, if the element is actually part of a form you can just call the .submit() method on that form. Which would actually avoid having to click on the hidden button entirely.

Selenium Web Driver click action

I have a code in selenium which dynamically clicks on the tabs in menu page. And sometimes happens that tab is not clickable(it is just a plain td with span inside) and when Web Driver tries to click on this tab, my program gets frozen (no exception thrown or anything).
I can't avoid clicking on tabs like that but I would like to somehow prevent that freeze after click. So can I set some timeout or tell to selenium what to do if element is not clickable?
driver.findElement(By.xpath(
format("//span[#class='rf-tab-lbl'][text()='{0}']",
navigation.getGroup()))).click();
//if the tab is currently selected (hence is not clickable) selenium won't click
// on it and program freezes
Please try another page from a different website. It might be a JS looping issue.
I encountered such an issue in HtmlUnit for some URLs.
I raised issue in HtmlUnit user group.
They told me that JS infinite loop was causing freez.
Time out did not work for me as well.
I tried to apply my own time out. That did not work too.
Refer following question for applying own timeout
HtmlUnit WebClient Timeout
If it works for another website, problem might be site specific.
You can attach source of Selenium in eclipse and check / debug where it is getting stuck.
I did same for HtmlUnit. I reached to parse method, which did not come out.
If still does not work, contact Selenium support.

Categories

Resources