I need it to move a slider bar to the right. I've seen on other posts that WebElement is used but I'm unsure still.
here is what I tried:
WebElement Slider = driver.findElement(By.cssSelector("#tipAmount"));
Thread.sleep(3000);
Actions moveSlider = new Actions(driver);
Action action = (Action) moveSlider.dragAndDropBy(Slider, 30, 0).build();
moveSlider.perform();
I haven't done many Actions but from what I can tell from looking at a sample it looks like you need to do
action.perform();
instead of
moveSlider.perform();
Related
I'm trying to swipe to right or left, but there's no button or element to click on it to swipe. The only option I have is to swipe to left or right is to hold the mouse and go to each side to swipe.
I've tried this method but it doesn't work for me:
Actions action = new Actions(driver);
action.clickAndHold(homePage.HeroImage).build().perform();
//you need to release the control from the test
Thread.sleep(2000);
action.moveToElement(homePage.HeroNext).release();
Thanks for your help :)
(HeroImage is the image that is showing now and HeroNext is the next image that i want to scroll into and both are visible)
I also tried this code, but it doesn't work either.
try {
for (int kk=0; kk<=6; kk++){
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "right");
js.executeScript("mobile: scroll", scrollObject);
}
System.out.println("Swipe Successfully");
}
catch (Exception e)
{
System.out.println("Image swipe was not successfull");
}
Have you tried doing your build and perform at the end?
Actions action = new Actions(driver);
action.clickAndHold(homePage.HeroImage);
//you need to release the control from the test
Thread.sleep(2000);
action.moveToElement(homePage.HeroNext).release();
action.build().perform();
I don't believe the sleep will be a part of the action as you intend it. Perhaps divide up into 2 actions if the hard wait is necessary. You might also be able to use dragAndDrop or dragAndDropBy. One drags to a target element the other to a target location
Use Drag and Drop with x and y coordinates:
Actions action = new Actions(driver);
action.dragAndDropBy(homePage.HeroImage, 200, 0).build().perform();
Here driver will perform drag and drop on HeroImage element horizontally. If you want to drag and drop vertically set x=0 and y="some range".
Hope it works.
Thank you.
First post ...
I discovered javascript and selenium, I'm trying to make a left click of a duration of 1 or 2 seconds.
It is easy to make a right click or a double click, but how to make a long click?
Thank you for your support.
Double click is ok :
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.ID ("Element ID"));
action. doubleClick (link).perform();
Click with executeScript ok:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(script).click();",Arguments);
At this point I have no track for the long click ...
You can try this below option
public void loingClick(WebDriver driver,WebElement element, int numberOfSeconds) throws Exception
{
Actions action = new Actions(driver);
action.clickAndHold(element).build().perform();
Thread.sleep(1000*numberOfSeconds);
action.moveToElement(element).release();
}
I have one button section product, but it disables when hover mouse on section product then the button is enabled. I don't know write script do enable a button when Selenium WebDriver finds it.
#Test(priority = 4)
public void TestSelectedItem() {
driver.findElement(By.xpath("/html/body/app-root/ng-component/div/product-list/div/div/div/div/div[2]/div/div[3]/div/div[1]/a")).click();
Select drpItem = new Select(driver.findElement(By.id("pa_colors")));
drpItem.selectByIndex(2);
driver.findElement(By.className("add_to_cart_button")).click();
}
Please help me.
I will give you code in Python which do hover action on the element:
hover_company = driver.find_element_by_xpath("your XPath")
driver.execute_script("arguments[0].scrollIntoView(true);", hover_company)
hover = ActionChains(driver).move_to_element(hover_company)
hover.perform()
First two lines scroll to your element, last two lines -- do hover action on the element.
After this action -- you can click on the button. Try on.
Code in Java:
hover_company = driver.findElement(By.xpath("your XPath"));
driver.executeScript("arguments[0].scrollIntoView(true);", hover_company);
Actions hover = new Actions(driver);
hover.moveToElement(hover_company).perform();
I am trying to click a mouse hover link using the code below. The webdriver (v.2.35) doesn't throw any error but the element isn't clicked. Can somebody help me figure out what's wrong?
String URL = "http://www.kgisliim.ac.in/"
String menu ="Alumni>Register"
driver.get(URL);
String[] menuItems = menu.split(">");
Actions actions = new Actions(driver);
WebElement tempElem;
for (int i =0 ; i< menuItems.length ; i++) {
tempElem = driver.findElement(By.linkText(menuItems[i].trim()));
actions.moveToElement(tempElem).build().perform();
}
actions.click();
actions.perform();
NOTE: The above code works fine in the below scenario
String URL = "http://www.flipkart.com/"
String menu ="Clothing>Jeans"
You can try this:
WebDriver driver=new FirefoxDriver();
driver.get("http://www.kgisliim.ac.in/");
Actions actions=new Actions(driver);
WebElement menuHoverLink=driver.findElement(By.linkText("Alumni"));
actions.moveToElement(menuHoverLink);
//driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement subLink=driver.findElement(By.cssSelector(".options>ul>li>a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Since the menu on http://www.kgisliim.ac.in/ takes a second to slide out, you could add a WebDriverWait to make sure the submenu has time to become visible before moving the cursor to it. Try replacing the first line in your for loop with the following line. This will wait a maximum of 5 seconds for the submenu (but will return the WebElement as fast as possible within that time).
tempElem = new WebDriverWait(driver, 5).until(ExpectedConditions
.elementToBeClickable(By.linkText(menuItems[i].trim())));
I stumbled across a similar issue recently, with phantomJS and ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).
You can check the window size with
driver.manage().window().getSize()
And you can change it with
driver.manage().window().setSize(new Dimension(width, height));
I am working on Walmart automation using Selenium WebDriver. I have written a function to hover over the Departments Menu "Home , Furniture & Patio" so that it gets highlighted and I can click on "Appliances" link. Here is the function that I have written but it does not seem to hover over the element.
public void NavigateDepartments(){
WebElement ApplianceLink = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions myMouse = new Actions(driver);
myMouse.moveToElement(ApplianceLink).build().perform();
ApplianceLink.click();
}
I also tried giving absolute path for
Xpath("/html/body/div/div/div[3]/div/div/div/ul/li[3]/div/div") to find the element and it did not work either. Am I missing anything ?
You should first hover on the main menu, then move to the new element
WebElement menu = driver.findElement(By.xpath("//path to *appliance*"));
WebElement parentMenu = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions builder = new Actions(driver);
builder.moveToElement(parentMenu).moveToElement(menu).click().build().perform();