Selenium Webriver:- How to click on this link? - java

I have to take cursor on "Tickets" link and then click on the "Statuses".
As soon as i take cursor outside this window/frame this screen gets closes and i again have to take cursor on "Tickets" link.
HTML is:-

To Mouse Hover over the element with text as Tickets and then to click on the element with text as Statuses you can use the following locator strategies:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement tickets = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#title='Tickets']")));
Actions actions = new Actions(driver);
actions.moveToElement(tickets).build().perform();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Statuses"))).click();
Optimizing the code you can use:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#title='Tickets']")))).build().perform();
new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='ember-view' and text()='Statuses']"))).click();

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

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

Wait until element is disabled

I use this code to click on a dropdown:
// Wait element to be clickable
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id(dropDownId)));
// Click dropdown to select item
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = driver.findElement(By.id(dropDownId));
wait.until(ExpectedConditions.elementToBeClickable(ele));
ele.click();
I get error element click intercepted: Element... probably because there is a layer which displayed Login bar. After that the element is disabled and the code throws exception.
Is it possible to implement a listen which waits for the element not to be disabled?
Is this what you are looking for -
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("YourXpath"))).isEnabled();

Unable to click on submenu using Selenium 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);

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