I am trying to hover over a main menu and select a submenu using java selenium, i got it to hover over the menu but cant select the sub menu, if i try to find by linktext i always get the error "does not exist " if i use xpath the says build successful but does not open up the new page. Here is my code for it so far
System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
webDriver.navigate().to("https://www.skiutah.com");
String NavTo = "DEALS";
String pathx = "//*[#id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(element).perform();
action.moveToElement(el).click();
//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.linkText("Deals"));
//Initiate mouse action using Actions class
Actions builder = new Actions(driver);
// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();
// wait for max of 5 seconds before proceeding.
// until this submenu is found
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a")));
//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[#id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a"));
menuOption.click();
This works for me first time, but if repeated for other menu item then it cant find or something.
WebElement menu = driver.findElement(By.your_locator);
WebElement sub_menu = driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();
In WebDriver we have given option to control Mouse events. Try this piece of code. This should serve the purpose.
driver.get("https://www.skiutah.com/");
WebElement deals = driver.findElement(By.xpath("//a[#title='Deals']"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
Locatable hoverItem = (Locatable) deals;
mouse.mouseMove(hoverItem.getCoordinates());
WebElement beginner = driver.findElement(By.xpath("//a[text()='Beginner']"));
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(beginner));
Locatable clickItem = (Locatable) beginner;
mouse.mouseDown(clickItem.getCoordinates());
mouse.mouseUp(clickItem.getCoordinates());
System.out.println(driver.getTitle());
To use mouse over action we need to use build.perform. It is called as action chaining which ensure that its perform actions together at the end. Or you can swap the line as below and it should work for you. I tried looks good.
String NavTo = "DEALS";
String pathx = "//*[#id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
String NavTo = "DEALS";
String pathx = "//*[#id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
action.moveToElement(element).perform();
I think the way you are hovering and clicking on a sub-menu is not seems correct.
You haven't shared your html so its little bit tedious to check element you have located are correct or not. If all fine, Try following code which might help you -
WebElement menu = driver.findElement(By.your_locator);
WebElement sub_menu = driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();
Explaination :-
Here build() method is used to compile all the list of actions into a single step and ready to be performed
First Mousehover to the main menu and then click any of the submenus.
WebDriverWait Wait = new WebDriverWait(driver,10);
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[#id='top_menu']/ul/li[4]/a"))));
Actions mousehover = new Actions(driver);
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform();
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals"))));
driver.findElement(By.linkText("All Deals")).click();
I'm trying to 'click' the sub-menu. Need to mouse over on to the main menu and it takes few seconds to load the sub-menu. then i need to locate the sub-menu and click it.
Here is the code which i used
Actions ac = new Actions(dr);
WebElement we = dr.findElement(By.xpath(".//*[#id='ddtopmenubar']/ul/li[1]/a"));
ac.moveToElement(we).build().perform();
WebDriverWait wait = new WebDriverWait(dr, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='dataingestionsubmenu']/li[2]/a")));
WebElement e= dr.findElement(By.xpath(".//*[#id='dataingestionsubmenu']/li[2]/a"));
e.click();
but it doesn't seems to work out.
getting exception as :
org.openqa.selenium.WebDriverException: performActions
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
when i do the same in debug mode, then i'm able to click submenu.
Related
The website is https://cloudwise.nl/ I'm trying to click on Dit is Cloudwise > Alle Cludwisers with Selenium using Java. It's a hoverable drop-down menu so I saw people handle this situation with the help of waiting until the presence of Element Located functions. so that's my code piece:
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement menu = wait.until(ExpectedConditions.presenceOfElementLocated((By.xpath("//a[#class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
WebElement submenu = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]")));
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();
But the test still passes and it does not click. What could I be doing wrong?
To Mouse Hover on Dit is Cloudwise and then to click on Alle Cludwisers you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following locator strategies:
Using xpath:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Dit is Cloudwise']")))).build().perform();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Dit is Cloudwise']//following::ul[1]/li/a"))).click();
Try changing presenceOfElementLocated with visibilityOfElementLocated.
Also, looks like
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();
Can be changed with
action.moveToElement(menu).moveToElement(submenu).click().build().perform();
#Erthan Yumer, you can achieve this in the way your initial implementation with the slight change as given below:
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement menu = wait.until(ExpectedConditions
.presenceOfElementLocated((By.xpath("//a[#class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
action.moveToElement(menu)
.moveToElement(wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))))
.click().build().perform();
I'm trying to change the text direction in the google search box from (right-to-left) to (left-to-right) using selenium before actually sending keys to it.
Google automatically sets the direction (right-to-left) because of my location and language.
i tried this code
driver.get("https://www.google.com.eg/");
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").perform();
But no matter what it doesn't work i also tried :
box.sendKeys(Keys.chord(Keys.LEFT_CONTROL, Keys.LEFT_SHIFT));
I can't seem to find a way in Selenium to press only 2 modifier keys without letters and get results
Try adding .build() so that your code will be
driver.get("https://www.google.com.eg/");
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").build().perform();
And if this still not work try adding an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 20);
driver.get("https://www.google.com.eg/");
wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").build().perform();
I'm trying to click on a submenu in a webpage. Submenu will be displayed when I just hover over the main menu. I have referred many solutions and it sill doesn't work.
And there are no iframes on the page.
Below is the code:
Actions ac = new Actions(d);
WebElement we = d.findElement(By.xpath("(//ul[#id='menu-top-menu']//li)[1]"));
ac.moveToElement(we).perform();
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement we2 = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("(//ul[#id='menu-top-menu']//li//ul//li[2]//a)[1]")));
ac.moveToElement(we2).click().perform();
Can someone please help me out with it?.
If it's not necessary to simulate mousein and click, you can get the href value of a element and direct access with driver.
You just need to check if element is present.
String xpathAction = "//ul[#id='menu-top-menu'] /li[1] /ul /li /a[text() = 'Action']";
WebDriverWait wait = new WebDriverWait(driver, 10);
String hrefAction = wait.until(
ExpectedConditions.presenceOfElementLocated(By.xpath(xpathAction))
).getAttribute("href");
driver.get(hrefAction);
I have written below code to drag an element and add it in workspace. There is no error in console window however drap drop is not performed on chrome browser.
WebElement dragElement = driver.findElement(By.xpath("//*[#id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement dropElement = driver.findElement(By.xpath("//*[#id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);
act.clickAndHold(dragElement).moveToElement(dropElement).release().build().perform();
I have tried multiple times but not able to succeed. Please provide your inputs
You can try give the location of the element
act.clickAndHold(dragElement).perform();
act.moveToElement(dropElement, dropElement.getLocation().getX(), dropElement.getLocation().getY()).perform();
act.release(dropElement).perform();
This is another method provided in the Selenium documentation here: http://www.seleniumhq.org/docs/03_webdriver.jsp#drag-and-drop.
WebElement dragElement = driver.findElement(By.xpath("//*[#id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
WebElement dropElement = driver.findElement(By.xpath("//*[#id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
(new Actions(driver)).dragAndDrop(dragElement, dropElement).perform();
You can use below code for drag and drop but I suggest you to optimize your xpath. It might be the real problem for you.
WebElement source = driver.findElement(By.xpath("//*[#id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement target = driver.findElement(By.xpath("//*[#id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);
Actions builder = new Actions(driver);
Action mouseOverHome = builder.dragAndDrop(source, target).build();
mouseOverHome.perform();
I had a similar issue in Firefox and resolved it by adding an extra movement instruction in front of the moveToElement() instruction, like this:
private void dragAndDrop(WebElement element, WebElement target) {
Actions builder = new Actions(driver);
builder.clickAndHold(element);
builder.moveByOffset(20,20); // THIS was the critical part for me
builder.moveToElement(target);
builder.release();
builder.perform();
}
I want to mousehover over image icon, then dropdown list is displayed later want to click on first option from drop-down list.
I tried with all these options but non of them work for me. Please suggest
Actions act = new Actions(driver);
WebElement iconhover =driver.findElement(By.className("insertItems"));
act.moveToElement(iconhover).click().build().perform();
WebElement ModulesAndTopics = driver.findElement(By.xpath("//*[#title='Topics']"));
ModulesAndTopics.click();
Another try
driver.switchTo().window(subwindow);
WebElement element = driver.findElement(By.className("insertItems"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
You need to hover over the dropdown list and perform a click.
Actions act = new Actions(driver);
WebElement iconhover =driver.findElement(By.className("insertItems"));
act.moveToElement(iconhover).click().build().perform();
WebElement ModulesAndTopics = driver.findElement(By.xpath("//*[#title='Topics']"));
act.moveToElement(ModulesAndTopics).click().build().perform(); //hover and click
Please try this, if not solved your problem then please provide me your HTML, So I can check:
new Actions(driver).
moveToElement(driver.findElement(By.className("insertItems")))
.build().perform();
Thread.sleep(2000);
//If //*[#title='Topics'] is xpath of your dropdown select tag
Select sel=new Select(wd.findElement(By.xpath("By.xpath("//*[#title='Topics']")")));
sel.selectByIndex(Index_OF_Option);
I think you are dealing with a menu item. I am assuming className = "insertItems" is unique.
//Locate the image icon
WebElement iconhover = driver.findElement(By.className("insertItems"));
//Hover mouse above the image icon - **DONT click**
Actions builder = new Actions(driver);
builder.moveToElement(iconhover).build().perform();
//Locating the first menu item - *plz use tagname instead of * in xpath* as its preferable
WebElement modulesAndTopics = driver.findElement(By.xpath("//*[#title='Topics']"));
modulesAndTopics.click();
Please note that sometimes the drop down list takes some time to open so its advisable to use Explicit Wait while locating the menu item - "modulesAndTopics", like this:
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement modulesAndTopics = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#title='Topics']")));
modulesAndTopics.click();
Please try the below code, it seems you have not used Select class:
WebElement iconhover = driver.findElement(By.className("insertItems"));
Actions builder = new Actions(driver);
builder.moveToElement(iconhover).build().perform();
WebElement abc=driver.findElement(By.xpath("//*[#title='Topics']");
Select drop= new Select(abc);
drop.selectByVisibleText("xyz");