how to swipe to right or left with selenium? - java

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.

Related

How to Automate Maps with Selenium Java? or with Page Co-Ordinates. Pleas show me is there any other way to Automate Maps

I'm Trying to capture the Co-ordinates of Maps to do some action on Maps.
wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.`path`("//button[contains(text(),'Add Tract')]"))).click();
Utils.scrollUp();
Thread.sleep(10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("TimeZoneId")));
//Timezone is the area which I'm trying to capture the Co-Ordinates
Point point1 = timezone.getLocation();
SOP("Element's Position from left side is: "+point1.getX()+" pixels.");
SOP("Element's Position from top is: "+point1.getY()+" pixels.");
}
if your map has <canvas> tag, then try
To perform using Actions chains, below is an example C# code similar to Java
IWebElement canvas = driver.FindElement(By.Id("TimeZoneId"));
int xCo = canvas.Location.X;
int yCo = canvas.Location.Y;Actions action = new Actions(driver);
action.MoveToElement(canvas, 1 + xCo, 2 + yCo).Click().Build().Perform();
Try OpenCV
If you are testing overlays on map. use JavaScriptExecutor and by adding hooks to your code in order to perform actions in your Map.
Try with Sikuli (personally, I have not used this. Needs some research)

How to enable button do verify in Selenium WebDriver?

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

Selenium WebDriver Slider

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

Unable to click a mouse hover link using Selenium WebDriver with Java

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

Scrolling using Selenium WebDriver with Java

I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.
So when I click on certain elements that are present below the current visible region of the browser, selenium tries to scroll the element into view and click them.
But because of the auto scrolling as such the elements are scrolled behind the floating header and when any action is performed on them, the elements in the page header get clicked.
is there any way to limit the default scroll of the WebDriver?
Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
You can scroll to the necessary location using javascript You need to use the scrollTo method rather than the scrollBy method for it to work.
public void scrollToElement(By by) {
Locatable element = (Locatable) selenium.findElement(by);
Point p= element.getCoordinates().getLocationOnScreen();
JavascriptExecutor js = (JavascriptExecutor) selenium;
js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
}
Scroll to top can be done:
private void scrollToTop() {
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("window.scrollTo(0, 0);");
}
Simple use the
.sendKeys(Keys.PAGE_DOWN);
when your element was visible, just click on it, by .click(element).perform();
for me work something like this:
clicker = new Actions(driver);
clicker.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(1000);
clicker.click(button).perform();
Thread.sleep(1000);
For scrolling down:
System.setProperty("webdriver.chrome.driver",
"/home/shreetesh/chromedriver");
WebDriver driver = new ChromeDriver();
String url = "https://en.wikipedia.org/wiki/Main_Page";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0, 25000);");
To scrolling up just replace the value of scroll with (2500, 0).
Use below code for scrolling up and scrolling down
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));
// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 50;
for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
try {
// this causes a gradual drag of the scroll bar, 10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
} catch(Exception e1){}
}
// now drag opposite way (downwards)
numberOfPixelsToDragTheScrollbarDown = -50;
for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
// this causes a gradual drag of the scroll bar, -10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
}
I recently had this problem due to a Drupal menu blocking the element when I ran this code:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
}
After referencing this page, I updated to set the boolean to false using this code, and it works great:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
}

Categories