I am looking to click a menu link, but the link only appears if the cursor hovers above it stretching the drop down. Therefore, the automation is unable to click it like it would normally with my click function. I did some research and used moveToElement, and clickAndHold. The latter has given me some hope, but it is far from perfect. I am finding it not clicking at all half the time, and sometimes it does click but clicks a different menu link in the drop down. Any ideas how I can make it work 100% of the time?
public String hoverClick(String object, String data){
APP_LOGS.debug("Moving the mouse");
try{
WebElement tab;
WebElement link;
tab = driver.findElement(By.xpath("//a[contains(#href, 'FOO')]"));
link = driver.findElement(By.xpath("//a[contains(#href, 'BAR')]"));
Actions act = new Actions(driver);
act.clickAndHold(tab).click(link).perform();
return Constants.KEYWORD_PASS;
}catch(Exception e){
return Constants.KEYWORD_FAIL+"Unable to move the mouse/click"+e.getMessage();
}
}
Thank you.
Please give a try with following:
act.moveToElement(tab).moveToElement(link).click(link).perform();
Here, we hover over the element which populates dropdown(and which contains element to be clicked on) and then move to element to be clicked on and then click it.
Related
{ actions.clickAndHold(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.DOWN)
.sendKeys(Keys.ENTER).perform(); }
enter image description here
Can't you use something like this? Obviously the selectors I used can be improved. I am not sure why we are clicking and holding and then pressing keys down. When we do this manually, that is not the action that we are taking. We basically move to the Rankings menu, then move to the womens link, then click if needed.
driver.get("https://www.cricbuzz.com");
WebElement rankingMenu = driver.findElement(By.xpath("//a[contains(text(),'Rankings')]"));
WebElement womensSelect = driver.findElement(By.xpath("//a[#title='ICC Rankings Women']"));
Actions action = new Actions(driver);
action.moveToElement(rankingMenu).moveToElement(womensSelect).click().build().perform();
I want to right click and go the 5th option that is "copy link address".
I have tried the following code and this is the only thing i could i could find on then internet
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.xpath("//*[(#id = \"u_0_1n\")]"));
TimeUnit.SECONDS.sleep(2);
actions.contextClick(elementLocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
This code actually scrolls the page downward instead of moving down the right click menu as if the right click menu was never there.
Using context clicks is not something I would recommend as it violates Parallel Testing Best Practices
The tests need to be small, atomic, and autonomous and your current approach assumes that the browser has to be in focus. It means that you will neither be able to do anything while the test is running nor run the tests in parallel.
So instead I would suggest:
Extract href attribute from the link you want to click
Use JavascriptExecutor and Window.open() function in order to open the link in the new tab
Switch to the new tab
Example code:
WebElement link = driver.findElement(By.xpath("//*[(#id = \"u_0_1n\")]"));
String url = link.getAttribute("href");
driver.executeScript("window.open('" + url + "');");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().reduce((f, s) -> s).orElse(null));
Please look into the attached screenshot
I need to select all the data in 1st box at one time.Currently it allows me to select First name and then click on Arrow and then i need to click on Middle name and click on Arrow to move to box 2.I need to do individually for all the data in Box1 to move to Box2.Also i tried drag and drop Its not working Even manually also its not allowing to drag/drop from Box1 to Box2
Can we select all the elements in the box 1 at a time and then click on arrow to move to box2?Please help me out with this issue..
(Note : Below is the Html code for the Box 1)
`
As per provided comment xpath
/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option
will retrieve list of all options nothing but list of webelements. so use findElements here and collect that list
List<WebElement> elements = driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option"));
//use loop here
System.out.println(elements .size());
for(int i=0;i<=elements .size();i++) {
//use click on option
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option")).click(); //it will click first option by default
//write command to click on arrow
//so it will loop upto list size nothing number of options and always select first and clicks on >>
}
thank you,
Murali
You can achieve this solution very easy by Jquery.
the following code very useful to you.
$().ready(function()
{
$('#right_arrow_id').click(function()
{
return
!$('#firstboxid_here option:selected').clone(true).appendTo('#second_box_id_here');
});
for removing options from second box.
$('#left_arrow_id_here').click(function()
{
$('#second_box_id_here option:selected').remove();
});
});
By looking at the first xpath "/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option[i]", I understood that it is implemented like a dropdown. Hence, you can use "Select" class in selenium. Please find the below code for the same.
// Initializing the Select class
Select names = new Select(driver);
// Retrieving all the options in the dropdown
List<WebElement> allNamesList = names.getOptions();
// Looping through the list
for(WebElement eachName : allNamesList) {
// Clicking on each element in the first box
eachName.click();
// Your Code to click on Arrow button
}
Hope this helps.
I am using WebDriver 2 with Java.
I am testing a menu that has hidden sublinks that are visible once a user hovers over them. I need to hover over the menu, click on each link within the menu, go the appropriate page, return back to the original page and repeat all the steps for the next link. I can do this using the following method which does that for each sublink separately. My goal is to create a method that will do all those steps using a loop, that can be re-used for similar menus. Here is the method that I have right now without a loop (I have created custom methods hoverOver and waitForElementVisibility).
hoverOver is:
public void hoverOver(WebElement element){
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
new WebDriverWait(driver,10);
}
waitForElementVisibility is:
public void waitForElementVisibility(WebElement element){
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(element));
}
And the method for menu and sublinks is(this is the method that I want to create as a loop):
public void checkLinksInTopMenu(WebElement menu, WebElement sublink1,
WebElement sublink2, WebElement sublink3){
hoverOver(menu);
waitForElementVisibility(sublink1);
sublink1.click();
driver.navigate().back();
hoverOver(menu);
waitForElementVisibility(sublink2);
sublink2.click();
driver.navigate().back();
hoverOver(menu);
waitForElementVisibility(sublink3);
sublink3.click();
driver.navigate().back();
}
Please help me out, thank you in advance.
if any exceptions, what is the exception?
Actions class will work for mouse over.
no need of wait statement in hoverOver method.
try like the following
mouseover followed by clicking on submenu item.
builder.moverToElement(webElement).click(webElement).build().perform();
if any exceptions, what is the exception?
Actions class will work for mouse over.
no need of wait statement in hoverOver method.
try like the following
mouseover followed by clicking on submenu item.
builder.moverToElement(webElement).click(webElement).build().perform();
if above code works then pass two arguements (webelement1, webelement2) to the method
webelement1 to hover the main menu
webelement2 to click on the submenu item
you can do this by simply calling a method with two parameters.
please let me know if you need any help.
I have used selenium 2.31.
I have used Actions class for mouse movement. Using this I moved the mouse over a menu and its submenu appeared only for a fraction of second unlike with older version of Firefox .
Because of this issue I cannot select the sub menu using driver.findElement as it throws an exception "element cannot be scrolled into view".
Is there any solution for this?
With the actions object you should first move the menu title, and then move to the popup menu item and click it. Don't forget to call actions.perform() at the end. Here's some sample Java code:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Another way to go about this is to use Selenium's JavaScript Executor to force the style of the element to be displayed.
An Example of this would be along this lines in C#
//Use the Browser to change the display of the element to be shown
(IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block");
//navigate to your link that is now viewable
driver.FindElement(By.Xpath('//LinkPath')).Click();
From there, you can find the XPath to your element and use selenium to click on the element. You can cascade this to find children of your main element as well
//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'");
Note that this is only possible if you have a hover element that changes the display style when hovered over.
Try this code...
It's c sharp code...
//Webelement is the main menu Link
webElement = driver.FindElement(By.XPath("Your element xpath"));
Actions act = new Actions(driver);
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//This line will help you to hold menu
//This web element is the sub menu which is under main menu
webElement = driver.FindElement(By.XPath("Sub menu path"));
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//Holds menu
//This web element is the option you have to click
webElement = driver.FindElement(By.XPath("Path"));
webElement.Click();
This will be helpful if you are using Ruby.
1.First you need to find element by xpath or id.
2.Then use the method action.move_to().perform.
Here is the code:
hover = WAIT.until{$driver.find_element(:xpath,"xpath")}
driver.action.move_to(hover).perform
This answer helped solve my problem.
My challange was to find a link under a menu option.
The link was not visible until I hovered over the Menu.
This crucial part for me was finding out that in addition to hovering over the menu, I next had to hover on the link in order to interact with it.
List<WebElement> list = driver.findElements(By.xpath("//a"));
for (int i=0;i<list.size();i++){
if(list.get(i).getText().equalsIgnoreCase("cacique intimates M"))
{
new Actions(driver).moveToElement(list.get(i)).click().build().perform();
System.out.println("Clicked on Parent Category");
new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform();
break;
}
}