Unable to click on submenu using Selenium Java - java

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);

Related

Selenium test passes on hovering drop-down menu item click but does nothing

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 can't click on an element

the page is fully loaded and the element is located, but for some reason it can't be clicked, I can't understand why.
My test:
#Test
public void test(){
chromeDriver.get("https://alfabank.ru/currency/");
WebDriverWait wait = new WebDriverWait(chromeDriver, 8);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = chromeDriver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
clicking.click();
}
Full xpatch it doesn't work either:
WebElement clicking = chromeDriver.findElement(By.xpath("/html/body/div[1]/div/div[6]/div/div/div/div/div[1]/div[1]/button[2]"));
Exception:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (539, 1199)
None of the existing answer really explain what is their code is about.
Even if you launch the screen in full screen, USD is not in Selenium view port.
Also JS is only recommended when nothing works.
I am doing with Selenium class, actions, please see below :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://alfabank.ru/currency/");
boolean b = wait.until(ExpectedConditions.titleIs("Курсы валют — «Альфа-Банк»"));
if (b) {
new Actions(driver).moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-test-id='caption']")))).build().perform();
WebElement clicking = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clicking);
}
I use JS click, don't know why cannot click normally.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true); arguments[0].click()", clicking);
Apply scrollIntoView and then click on the USD using JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://alfabank.ru/currency/");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement usdoption = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
js.executeScript("arguments[0].scrollIntoView(true);", usdoption);
js.executeScript("arguments[0].click();", usdoption);

How to press Ctrl + Shift in Selenium with Java

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();

Hover over menu and select sub menu in java Selenium

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.

Mouse hover functionality using selenium Webdriver

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");

Categories