I want to swipe in mobile web site using selenium on java.
I have tried the following code. But it doesn't help me out.
1. //Swipe Right to Left side of the Media Viewer First Page
WebElement firstPages = driver.findElement(By.id("media-list"));
TouchActions flick = new TouchActions(driver).flick(firstPages,-100,0,0);
flick.perform();
Result:- org.openqa.selenium.chrome.ChromeDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
2. //perform swipe gesture
TouchActions swipe = new TouchActions(driver).flick(0, -20);
swipe.perform();
3. JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new java.util.HashMap<String, Double>();
swipeObject.put("startX", 0.95);
swipeObject.put("startY", 0.5);
swipeObject.put("endX", 0.05);
swipeObject.put("endY", 0.5);
swipeObject.put("duration", 1.8);
js.executeScript("mobile: swipe", swipeObject);
}
Result: unknown error: swipe is not defined(..)
You could trigger an event via javascript eg. with jquery trigger
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#media-list').trigger('swipe', {startX: 0.95})");
Related
WebDriver driver = new ChromeDriver();
driver.get('https://www.nvidia.com/en-gb/shop/geforce/?page=1&limit=9&locale=en-gb');
WebElement notify = driver.findElement(By.id('cookiePolicy-btn-close'));
notify.click();
WebElement buyButton = driver.findElement(By.className('featured-buy-link link-btn'));
buyButton.click();
I am trying to swipe down a modal view in iOS using Appium with java.
I have tried this two ways unsuccessfully:
JavascriptExecutor js2 = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject2 = new HashMap<String, String>();
scrollObject2.put("x", "200");
scrollObject2.put("y", "550");
scrollObject2.put("direction", "down");
js2.executeScript("mobile: swipe", scrollObject2);
TouchAction action = new TouchAction(driver);
action.press(PointOption.point(200, 550)).moveTo(PointOption.point(200, 700)).release().perform();
What I am doing wrong? is another correct way to achieve this?
Thanks!
You should do it using js script executor.
No need to add coordinates like you did, just try something like this:
HashMap<String, String> scrollObject = new HashMap<>();
JavascriptExecutor js = driver;
scrollObject.put("direction", "down");
js.executeScript("mobile: scroll", scrollObject); //or "mobile: swipe"
public static void fingerSwipe(int startX, int startY, int endX, int endY, long timeInMillis){
PointerInput touchAction = new PointerInput(PointerInput.Kind.TOUCH, "touchAction");
Interaction moveToStart = touchAction.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY);
Interaction pressDown = touchAction.createPointerDown(PointerInput.MouseButton.LEFT.asArg());
Interaction moveToEnd = touchAction.createPointerMove(Duration.ofMillis(timeInMillis), PointerInput.Origin.viewport(), endX, endY);
Interaction pressUp = touchAction.createPointerUp(PointerInput.MouseButton.LEFT.asArg());
Sequence swipe = new Sequence(touchAction, 0);
swipe.addAction(moveToStart);
swipe.addAction(pressDown);
swipe.addAction(moveToEnd);
swipe.addAction(pressUp);
driver.perform(Arrays.asList(swipe));
}
I use selenium interactions package to perform a swipe using JAVA and appium. Try using something similar to above code in WebDriverIo for Appium versions - 1.15.0 and above. You just need to pass input parameters depending upon the swipe you want to perform.
'long timeInMillis' is the time period of the swipe.
I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);
I have a Javascript which is used to scroll the web element .
I want to convert it to selenium + java code.
I tried using java script executor but somehow I'm not able to get the desired result.
Here's the javascript that is working fine for me .
var x = window.content.document.getElementsByClassName("_PMgtb");
x[0].scrollTop += 100;
You can use the "org.openqa.selenium.interactions.Actions" class to move to an element:
WebElement element = driver.findElement(By.class("_PMgtb"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
You could use
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = null;
element = (WebElement) js.executeScript("return document.getElementsByClassName("_PMgtb")[0];", element);
js.executeScript("arguments[0].scrollTop = arguments[1];",element, 100);
I'm using the following to navigate a menu in Selenium. It works perfectly in Chrome, however in IE, it ends up clicking the menu below my target and the submenu item becomes completely inaccessible.
// Actions not supported by FireFox's Marionette Driver, use chrome or ie.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode"));
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_INVENTOR_a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
And here is where the drive is initialized
System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer64.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I would like to be able to click on the proper menu item to continue with my testing.
I managed to get a working solution by moving the cursor over from the offset. Not sure how well this will work across screens.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText(arg1));
//<span id="m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode">Inventory</span>
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
actions.moveByOffset(100, 10);
Thread.sleep(1000);
//actions.moveByOffset(45, 0);
WebElement subLink = driver.findElement(By.
id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_SRVITEM_a_tnode"));
actions.moveToElement(subLink, 100, 12)
.click().perform();