I just want to hover over the "Departments" drop-down list on the Amazon website. The code looks alright but the list is not showing. It's the Department drop-down list I am trying to show
Here's my code
driver = new ChromeDriver();
driver.get("https://www.amazon.com");
Actions actions = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("//span[#class='nav-line-2']"));
Thread.sleep(300);
actions.moveToElement(ele);
actions.perform();
actions.perform();
Looks like the xpath is not unique and with the same locator, locating 6 elements in the page. When we have more than one element with same locator, selenium go for first element. In your case, unfortunately 'Departments' is not the first element with that locator.
Change your xpath to below: [Tested & worked]
//span[#class='nav-line-2' and contains(.,'Departments')]
To Mouse Hover over the element with text as Departments you need to induce WebDriverWait for the desired element to be visible and use moveToElement() method in conjunction with perform() method and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class amazon_com_Departments {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.amazon.com");
WebElement department = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#id='nav-link-shopall' and normalize-space()='Departments']")));
new Actions(driver).moveToElement(department).perform();
}
}
Browser Snapshot:
Related
Tring to click on the input box, with script able to open the URL-https://jqueryui.com/datepicker/.In locator the id is available still getting No element foun.
Tried with thread.sleep as well
when i am running the script getting exception
package SeleniumWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingCalender {
public static void main(String[] args) throws InterruptedException {
WebDriver driver =new ChromeDriver();
driver.get("https://jqueryui.com/datepicker/");
//driver.manage().window().maximize();
Thread.sleep(1000);
driver.findElement(By.id("datepicker")).click();
Element you trying to access is inside an iframe.
So, to access it you need first to switch into that iframe, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.class("demo-frame")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("datepicker"))).click();
I tried the below code but it is not mouse hovering and clicking on 'Member login'
WebElement lgn = driver.findElement(By.id("ctl00_HyperLinkLogin"));
WebElement ssm = driver.findElement(By.xpath("//a[contains(text(), 'SpiceCash/SpiceClub Members')]"));
WebElement cgm = driver.findElement(By.xpath("//a[contains(text(),'Member Login')]"));
Actions a1 = new Actions(driver);
a1.moveToElement(lgn).moveToElement(ssm).moveToElement(cgm).click().build().perform();
To invoke click() on the element with text as Member login, first you have to Mouse Hover over the element with text as LOGIN / SIGNUP, then Mouse Hover over the element with text as SpiceCash/SpiceClub Members then induce WebDriverWait for the element with text as Member Login to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Spicejet_member_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.link#ctl00_HyperLinkLogin")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[#class='hide-mobile']/a[contains(.,'SpiceCash/SpiceClub Members')]")))).build().perform();
new WebDriverWait(driver, 7).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='hide-mobile']//ul/li/a[#href='https://book.spicejet.com/Login.aspx' and contains(.,'Member Login')]"))).click();
}
}
Browser Snapshot:
You can try to add waits between your moveToElement() calls
WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(), 10);
wait.until(ExpectedConditions.visibilityOf(element))
where the "element" is your menu that should appear on hover.
Or you can use ready solution Selenide framework which is built on top of the Selenium and has built in hover method and waits which help to handle page dynamics
By this link you can find an example of hover() method usage.
Trying to get the script to select the filter button on the top but can't seem to figure out how to input the XPath. I believe it has something to do with it be in a separate iframe.
package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mlvtwrew73.consilio.com/Relativity/");
driver.manage().window().maximize();
//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
WebElement objWE;
Thread.sleep(9000);
// objWE = driver.findElement(By.linkText("User Status"));
// objWE.click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
// objWE = driver.findElement(By.id("1"));
// driver.close(); will be used to close the site once all testing completes
}
}
Use ID locator -- it's more appropriate here (and faster than XPath):
WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_ctl00_itemList_FilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();
Looks like you're passing in an incorrect XPath. Try this one:
driver.findElement(By.xpath("//a[#id='ctl00_ctl00_itemList_FilterSwitch']")).click();
If there is then why you are using xpath.
Use id like this
driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();
I need more clarity regarding your question.
As I have understand, I think you need a requirement of XPath of the Filters image. If i am right, try this:
//div[#class='actionCellContainer']//a/img[#class='itemListActionImage']
I am a beginner in Selenium WebDriver and I'm using this test on the StackOverflow homepage. These will be the steps of my test:
Firstly, go to the homepage of the StackOverflow.
Then, move the mouse on the Users button such that it will be focused. (We don't have to click on it just hover the mouse over it.)
Now, find the active element on the screen (i.e. the element which is
currently focused) and then click on it.
I want the Users button to be clicked because it is focussed currently but that's not happening. Instead the Questions button is clicked on.
Here is my code for the same test.
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
I am not able to understand why I am not getting the expected output. Please help.
I don't know why Actions class is not focusing the element. Its an issue or something as so many time i have trouble with Actions class in Firefox browser.
Still you have one alternative way to Focus on desired element and then perform click on focused element i.e. JavascriptExecutor
Here you is your code :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('nav-users').focus();");
System.out.println(driver.switchTo().activeElement().getTagName());
driver.switchTo().activeElement().click();
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.
package nxusdata;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class MouseHoveringworpress {
public static WebDriver driver;
#BeforeTest
public void openinBrowser(){
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://wordpress.com/wp-login.php?redirect_to=https%3A%2F%2Fwordpress.com%2F");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void menus() throws InterruptedException{
driver.findElement(By.xpath("//*[#id='user_login']")).sendKeys("ritu7180");
driver.findElement(By.xpath("//*[#id='user_pass']")).sendKeys("jaiguruji123");
driver.findElement(By.xpath("//*[#id='wp-submit']")).click();
//*[#id='header']/a[2]/span
Thread.sleep(15000);
driver.findElement(By.xpath("//*[#id='header']/a[1]/span")).click();
//*[#id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]
Thread.sleep(4000);
driver.findElement(By.xpath("//*[#id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]")).click();
Thread.sleep(10000);
Set <String> windows =driver.getWindowHandles();
System.out.println(windows.size());
//Print the size of windows
Iterator<String> it = windows.iterator();
//iterate through your windows
while (it.hasNext()){
String parentwindow = it.next();
System.out.println("This is first window id "+parentwindow);
String childwindow = it.next();
System.out.println("this is second window id "+childwindow);
driver.switchTo().window(childwindow);
// driver.findElement(By.xpath("//*[#id='save-post']")).click();
Thread.sleep(4000);
WebElement menu = driver.findElement(By.xpath("//*[#id='menu-links']/a/div[3]"));
WebElement SUBMenu = driver.findElement(By.xpath("//*[#id='menu-links']/ul/li[2]/a"));
Actions action = new Actions(driver);
action.moveToElement(menu).perform();
Thread.sleep(2000);
action.click(SUBMenu).perform();
}
}
}
You can try something like this:
Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//*[#id='menu-links']/a/div[3]"));
actions.moveToElement(menu);
WebElement subMenu = driver.findElement(By.xpath("//*[#id='menu-links']/ul/li[2]/a"));
actions.moveToElement(subMenu);
actions.click().build().perform();
Learn more here.
// Locating the Main Menu (Parent element)
WebElement mouseHoverOnLocations = driver.findElement(By.partialLinkText("Locations"));
// Instantiating Actions class
Actions actions = new Actions(driver);
// Hovering on main menu
actions.moveToElement(mouseHoverOnLocations);
// Locating the element from Sub Menu
WebElement selectSantaClara = driver.findElement(By.linkText("Santa Clara"));
// To mouseover on sub menu
actions.moveToElement(selectSantaClara);
// build()- used to compile all the actions into a single step
actions.click().build().perform();