How to do Resize using Java and Selenium? - java

How to do Resize using Java and Selenium?
Link
Code I have been trying:
public static void main(String[] args) throws InterruptedException
{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.get("https://jqueryui.com/resizable/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.switchTo().frame(0);
Thread.sleep(2000);
WebElement resize = driver.findElement(By.xpath("//div[#id = 'resizable']/div[3]"));
//WebElement resize = driver.findElement(By.id("resizable"));
new Actions(driver).dragAndDropBy(resize, 400, 400).perform();
}
the this is the below error:
> Exception in thread "main"
> org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move
> target out of bounds

The code that you'd shared earlier, I have made a few changes in that, Please see the below code.
The web element is in iframe, therefore we will have to first switch to the iframe and then use action class dragAndDropBy with the following properties:
(WebElement source, int xOffset, int yOffset)
Code:
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://jqueryui.com/resizable/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.demo-frame")));
WebElement resizeButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.ui-icon-gripsmall-diagonal-se")));
action.dragAndDropBy(resizeButton, 100, 50).build().perform();

Related

selenium java NGWebdriver

I have tried everything to login into one site using Selenium webdriver java, but there is one "window", which I don't know how to call it , that I couldn't find one way to click in order to access it. Here, I open the firefox browser and lend on the parfumo.net webpage. The site loads "one window" with cookies settings...
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
There is a Iframe you have to first switch to it like below
driver.switchTo().frame("iframeId");
OR
driver.switchTo().frame("iframeName");
Once you switch to the frame than perform the click like below.
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
driver.switchTo().frame("sp_message_iframe_737779");
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
You can switch back to the main document like below:
driver.switchTo().defaultContent();

Selenium: Page gets refreshed after login

After the code clicks the action to login, the page gets refreshed and doesn't redirect to the next page. This only occurs for this website as I am redirected properly on other sites.
When I am giving the wrong credentials even the error message is not getting displayed.(Manually it works)
I am testing in Chrome with Selenium java.
Here is my code:
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://-----/tandem/login");
Thread.sleep(5000);
driver.manage().deleteAllCookies();
driver.findElement(By.id("login")).sendKeys("----");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys("----");
WebElement loginbutton = driver.findElement(By.xpath(".//*[#id='login-button']"));
Actions actions = new Actions(driver);
actions.click(loginbutton).perform();
}
}
To login in into the website https://outhouse.inhouseusa.com/tandem/login/ you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:
Code Block:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
//options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://outhouse.inhouseusa.com/tandem/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#login"))).sendKeys("sandy");
driver.findElement(By.cssSelector("input.form-control#password")).sendKeys("sandy");
driver.findElement(By.cssSelector("input.pull-right.button.button-primary#login-button")).click();

Slider error always Selenium Webdriver

Use of everything done. No output only shows an error. Have tried all these ways but it only shows an error. Totally lost it. Nothing is moving forward from yesterday. Kindly look into it and please let me know.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*
[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*
[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//WebElement target = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement source = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[1]"));
//a.dragAndDrop(source, target).build().perform();
WebElement slider = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement slider = driver.findElement(By.id("DivDepart"));
Actions a = new Actions(driver);
//a.dragAndDropBy(slider, 30, 0).build().perform();
//a.clickAndHold(slider).moveByOffset(30,
0).release(slider).build().perform();
//System.out.println("moved");
//JavascriptExecutor js = (JavascriptExecutor)driver;
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
//js.executeScript("window.scrollBy(200,0)");
}
}
Note :-
In your code I can see clearly that, you have not selected depart date and arrival date and you are directly clicking on "Search Now" button. Validation messages are appearing if you directly click on search button without selecting depart and arrival date. So webdriver is not able to find the slider that your are interested in. Code you have written for slider is working fine. Do not change it.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
//------- Corrections
driver.findElement(By.xpath("//*[#id='departCalendar_0']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[27]/li")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[31]/li")).click();
// Corrections--------
driver.findElement(By.xpath(".//*[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement target = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
WebElement source = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[1]"));
WebElement slider = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
((JavascriptExecutor) driver).executeScript("scrollBy(0,500);");
Actions a = new Actions(driver);
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
} }
Just Execute this code, I have executed this and working fine and as expected.
I just picked your code and executed once, After correction now it is working very fine. Your Slider code is perfect no need of change in it.
Issue was you had not written script for selecting dates[departure and arrival]
You can also try this code to move Slider :
Thread.sleep(5000);
Actions builder1 = new Actions(driver);
WebElement zero = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
builder1.dragAndDropBy(zero, 1000, 0).perform();
xpath of slider square :- //*[#id='slider-range']/span[1]
Please let me know if it is working at your end.
After entering From/To & date details when user clicks 'Search' button. Then moves to next screen. From there I believe you would like to move the slider. Then try following code and you may have to alter wait based on application need.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_PAGE_DOWN);
rob.keyRelease(KeyEvent.VK_PAGE_DOWN);

cant select the tab drag n sortabke in t he link http://demoqa.com/draggable/ (none of the tabs there)

I tried using all known element identifier to select the tabs in the drag n drop options in the link but constantly giving error object cant be located .
can anyone help me.
public class DragnDrop {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\Auto\\geckodriver-v0.11.1-win32\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
Actions builder = new Actions(driver);
WebElement know = driver.findElement(By.xpath(".//*[#id='tabs']/ul"));
builder.moveToElement(know, 10, 10 ).click().build().perform();
i tried to even use action to click at particular location none worked
As far as I can see, if it's the "Draggable + Sortable" tab you're trying to click, the below should allow that:
WebElement draggableTab = driver.findElement(By.id("ui-id-5"));
draggableTab.click();
I've tried the above (using C#) and it worked fine.
public class DragDrop {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir") + "\\src\\Browser_Driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")));
driver.findElement(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")).click();
// code works perfectly for demoqa site
Actions builder = new Actions(driver);
List<WebElement> list = driver.findElements(By.cssSelector("#sortablebox li"));
WebElement source = driver.findElement(By.id("draggablebox"));
WebElement dest1 = list.get(1);
WebElement dest2 = list.get(4);
builder.click(source).clickAndHold().moveToElement(dest1).moveByOffset(0, 10).release().build().perform();
Thread.sleep(2000);
builder.click(source).clickAndHold().moveToElement(dest2).moveByOffset(0, 10).release().build().perform();

How do I fill the email section using chrome driver

enter image description here
Can you help me to input text on the email box? The email box appears when I click on masuk button on the top, but I can't get to sendkeys on the email box.
H|ere is the url- www.tokopedia.com
And here is the code that does not work
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("login-ddl-link"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.findElement(By.id("login-ddl-link")).click();
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
}
This is because authorization form located inside iframe element. You need to switch to that frame at first and then handle input fields:
...
driver.findElement(By.id("login-ddl-link")).click();
Thread.sleep(2000);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes123");
...
To switch back you might need to use
driver.switchTo().defaultContent();
P.S. You don't need to click on input field to sent text to it, so driver.findElement(By.id("inputEmail")).click(); is redundant line
use this code it worked just fine for me on Chrome.
public static void main(String [] ar) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//*[#id='login-ddl-link']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
driver.findElement(By.xpath(".//*[#id='global_login_btn']")).click();
}

Categories