package com.s3sales.demo;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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 Settings_Area {
public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver driver=new FirefoxDriver();
driver.get("http://sssamriddhisales.com/crm");
Thread.sleep(1000);
driver.findElement(By.id("userName")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.id("password")).sendKeys("admin123");
Thread.sleep(2000);
driver.findElement(By.className("btn-success")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.linkText("Settings"));
WebDriverWait wait=new WebDriverWait(driver, 3);
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,100)");
wait.until(ExpectedConditions.elementToBeClickable(element));
Thread.sleep(1000);
Actions action = new Actions(driver);
action.moveToElement(element).moveToElement(driver.findElement(By.cssSelector("[data-id='area']"))).click().build().perform();
Thread.sleep(1000);
}
}
In Menu bar , I have a list of elements in that scroll down bar not moving .But, i want to select particular element is Area(Settings) .I have tried my below code . But, showing as Exception in thread “main” java.lang.ClassCastException ..Please me codeenter image description here
Try the below code:
driver.get("http://sssamriddhisales.com/crm");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(3, TimeUnit.MINUTES).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class, ElementNotVisibleException.class);
WebElement userName = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("userName"));
}
});
WebElement password = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("password"));
}
});
WebElement submit = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("//button[#type='submit']"));
}
});
userName.sendKeys("admin");
password.sendKeys("admin123");
submit.click();
WebElement settings = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("//a[#class='text-center']//span[text()='Settings']"));
}
});
Actions actions = new Actions(driver);
actions.moveToElement(settings).build().perform();
final WebElement element = driver.findElement(By.xpath("//li[#data-id='area']//a[text()='Area']"));
WebElement area = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return element;
}
});
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
area.click();
Using Thread.sleep() is not a good idea so I have removed it and replaced with FluentWait. You can change it if you want to.
The above code will login to the application, then do mouse over on the settings then clicks the Area.
Kindly try below code:
driver.get("http://sssamriddhisales.com/crm");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.findElement(By.id("userName")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.id("password")).sendKeys("admin123");
Thread.sleep(2000);
driver.findElement(By.className("btn-success")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath("//a[#class='text-center']//span[text()='Settings']"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
driver.findElement(By.xpath("//li[#data-id='area']//a[text()='Area']")).click();
Related
I am registering on a website and for OTP and signup link I am using yopmail so I have to toggle between two tabs everything is working fine but when I switch to second window using any browser webdriver is not focussing on the second tab and giving error as array index out of bounds I have triedit on both chrome and firefox browsers but still getting the same error
package Stripetesting;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class Plansinfirefox {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.gecko.driver", "/home/gt51022/Downloads/geckodriver-v0.32.0-linux64/geckodriver");
WebDriver driver = new FirefoxDriver();
String windowHandle = driver.getWindowHandle();
driver.get("https://app.ai/");
driver.navigate().forward();
driver.manage().window().maximize();
driver.navigate().forward();
Thread.sleep(4000);
driver.findElement(By.xpath("//*[#id=\"formBasicEmail\"]")).sendKeys("wond#yopmail.com");
Thread.sleep(2000);
driver.findElement(By.name("password")).sendKeys("Qwerty#1");
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
driver.navigate().forward();
driver.findElement(By.className("checkmark")).click();
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id=\"root\"]/div/div[2]/div/div/div[1]/div/div/form/button")).click();
driver.navigate().forward();
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div/div/div[1]/ul/li[2]/a")).click();
Thread.sleep(4000);
// Selecting the team from here //
driver.findElement(By.xpath("/html/body/div/div[2]/div[2]/div[3]/div/div/div[2]/div")).click();
driver.navigate().forward();
Thread.sleep(4000);
// Clicking on add new team member //
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[1]/div[2]/button")).click();
Thread.sleep(1000);
ArrayList<String> tabs3 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().newWindow(WindowType.TAB);
// Generating random email //
driver.get("http://www.yopmail.com");
// Clicking on random email generator //
driver.findElement(By.xpath("/html/body/div/div[2]/main/div[3]/div/div[2]/div/a[1]/div[2]/h3")).click();
Thread.sleep(4000);
// Clicking on copy to clipboard //
WebElement copytoclipboard = driver.findElement(By.xpath("(//span[#class=\"notmobile\"])[2]"));
Actions actions = new Actions(driver);
actions.click(copytoclipboard).doubleClick().perform();
// navigating to main window //
driver.switchTo().window(windowHandle);
Thread.sleep(4000);
driver.findElement(By.xpath("//*[#id=\"formPlaintextEmail\"]")).sendKeys(Keys.LEFT_CONTROL,"v");
// Selecting the role from here //
driver.navigate().forward();driver.findElement(By.xpath("/html/body/div[3]/div/div/form/div[2]/div[2]/div/div/div[1]/input[1]")).click();
driver.navigate().forward();
Thread.sleep(4000);
// Selecting the permissions from here //
driver.findElement(By.xpath("//a[#id=\"basic-typeahead-multiple-item-16\"]")).click();
Thread.sleep(2000);
//driver.findElement(By.xpath("(//span)[22]")).click();
driver.findElement(By.xpath("//button[#id=\"teams\"]")).click();
driver.navigate().forward();
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div[3]/div/div/form/div[2]/div[3]/div/div/div/div/div/span[1]")).click();
Thread.sleep(2000);
driver.navigate().forward();
driver.findElement(By.xpath("/html/body/div[3]/div/div/form/div[2]/div[3]/div/div/button")).click();
driver.findElement(By.xpath("//button[#class=\"btn btn-primary\"]")).click();
Thread.sleep(2000);
driver.manage().deleteAllCookies();
// If you want to sign out //
driver.findElement(By.xpath("//img[#class=\"rounded-circle\"]")).click();
driver.findElement(By.xpath("//a[#href=\"#\"]")).click();
Thread.sleep(4000);
// Opening new browser so commenting this code //
//driver.switchTo().newWindow(WindowType.WINDOW);
// User has received invitation //
System.setProperty("webdriver.chrome.driver", "/home/gt51022/Downloads/chromedriver_linux64/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.yopmail.com");
driver.findElement(By.xpath("//*[#id=\"login\"]")).sendKeys(Keys.LEFT_CONTROL,"v"); //replace the test email
//driver.get("http://www.yopmail.com");
driver.findElement(By.xpath("/html/body/div[1]/div[2]/main/div[3]/div/div[1]/div[2]/div/div/form/div/div/div[4]/button/i\n")).click();
Thread.sleep(2000);
driver.manage().window().maximize();
driver.switchTo().frame("ifmail");
driver.findElement(By.linkText("http://app.ambaram.ai/signup")).click();
driver.switchTo().window(tabs3.get(2));
driver.switchTo().activeElement();
driver.navigate().forward();
Thread.sleep(2000);
driver.findElement(By.className("form-control")).sendKeys("buyer");
driver.navigate().forward();
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[1]/div/div/form/div[2]/input")).sendKeys(Keys.LEFT_CONTROL,"v");
Thread.sleep(1000);
driver.findElement(By.className("checkmark")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"root\"]/div/div[2]/div/div[1]/div/div/form/button")).click();
//driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div[1]/div/div/p/a")).click();
driver.navigate().forward();
/*Thread.sleep(4000);
// Commenting this code for usage checking //
driver.findElement(By.xpath("//*[#id=\"fullName\"]")).sendKeys("Bestfit");
driver.navigate().forward();
driver.findElement(By.name("email")).sendKeys("Second#yopmail.com");
driver.navigate().forward();
driver.findElement(By.className("checkmark")).click();
driver.findElement(By.xpath("//*[#id=\"root\"]/div/div[2]/div/div[1]/div/div/form/button")).click();
//perform other operations.
// Stopping the robot class as we have tab already opened //
/*Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);*/
driver.switchTo().window(tabs3.get(1));
//driver.get("http://www.yopmail.com");
Thread.sleep(4000);
driver.navigate().refresh();
Thread.sleep(4000);
//driver.findElement(By.xpath("//*[#id=\"login\"]")).sendKeys("nu"); //replace the test email
//driver.findElement(By.xpath("/html/body/div[1]/div[2]/main/div[3]/div/div[1]/div[2]/div/div/form/div/div/div[4]/button/i\n")).click();
driver.switchTo().frame("ifmail");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String strCode = driver.findElement(By.xpath("//*[#id=\"mail\"]/div/table/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[5]/td/span")).getText(); //replace the locator for verification code
System.out.println("----------------");
System.out.println(strCode);
System.out.println("----------------");
//Actions actions = new Actions(driver);
actions.sendKeys(Keys.chord(Keys.LEFT_CONTROL, "c")).build().perform();
driver.switchTo().window(tabs3.get(2));
//driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
driver.navigate().forward();
driver.findElement(By.id("code1")).sendKeys(strCode);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
int i;
for (i = 1; i <= 6; i++) {
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[#class=\"otp btn btn-primary\"]")).click();
driver.navigate().forward();
driver.findElement(By.xpath("//input[#name=\"newPassword\"]")).sendKeys("Qwert#1");
driver.navigate().forward();
driver.findElement(By.xpath("//input[#name=\"comfirmNewPass\"] ")).sendKeys("Qwert#1");
driver.navigate().forward();
driver.findElement(By.xpath("//button[#class=\"otp btn btn-primary\"]")).click();
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("email")).sendKeys(Keys.LEFT_CONTROL,"v");
driver.navigate().forward();
driver.findElement(By.name("password")).sendKeys("Qwert#1");
driver.navigate().forward();
driver.findElement(By.xpath("//span[#class=\"checkmark\"]")).click();
driver.navigate().forward();
driver.findElement(By.xpath("//button[#class=\"btn btn-primary\"]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("(//span[#class=\"toggleHide\"])[2]")).click();
// User click on joining a team //
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/div[2]/span/div/button")).click();
//New Team Creation //
/*driver.findElement(By.xpath("(//span[#class=\"toggleHide\"])[2]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//button[#class=\"NewUserWrap secondaryBtn\"]")).click();
Thread.sleep(2000);
driver.findElement(By.name("teamLogo")).sendKeys("/home/gt51022/Downloads/Wallpapers/Good.jpg");
Thread.sleep(2000);
driver.findElement(By.name("name")).sendKeys("Creaters");
Thread.sleep(2000);
driver.findElement(By.xpath("//textarea[#name=\"description\"]")).sendKeys("Of good time");
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div[3]/div/div/form/div[3]/button[2]")).click();*/
// Changing the profile photo here //
Thread.sleep(1000);
driver.findElement(By.xpath("//img[#class=\"rounded-circle\"]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/div/div[2]/div[2]/header/div/div[3]/div[3]/div/div/a[1]")).click();
Thread.sleep(1000);
WebElement elementLocator = driver.findElement(By.name("profilePicture"));
actions.click(driver.findElement(By.name("profilePicture")));
//actions.moveToElement(elementLocator).click().perform();
driver.findElement(By.name("profilePicture")).sendKeys("/home/gt51022/Downloads/ca-times.brightspotcdn.jpeg");
//driver.findElement(By.name("profilePicture")).sendKeys("/home/gt8201/Downloads/Kolkata.jpeg");
driver.findElement(By.xpath("//button[#class=\"activateBtn btn btn-primary\"]")).click();
//driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/ul/li[1]/a")).click();
Thread.sleep(2000);
actions.click(driver.findElement(By.name("profilePicture")));
}
// Navigating back to the dashboard //
driver.findElement(By.xpath("//span[#class=\"toggleHide\"]")).click();
}
}
Here are two Methods, and then you can use tabnumber for switching tabs
public void switchTab(int tabNumber){
driver.switchTo().window(listTabs().get(tabNumber));
}
public List<String> listTabs(){
List<String> list = new ArrayList<String>();
for (String window: driver.getWindowHandles()){
list.add(window);
}
return list;
}
enter image description here**Right click is working but not able to click on Open link in new window option. **
public static void newwindow() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\TechExtensor\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
Thread.sleep(3000);
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.xpath("//*[#id=\"main_navbar\"]/ul/li[3]/a/span"));
actions.moveToElement(elementLocator);
Thread.sleep(2000);
actions.contextClick(elementLocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
Use Robot class.
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.xpath("//*[#id=\"main_navbar\"]/ul/li[3]/a/span"));
actions.contextClick(elementLocator).perform();
Thread.sleep(1000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
I have one button which sometimes does not do the actual click, due to that i wrote a code to handle it with a while loop where it will click until it finds out a new assignment title of the page. But this is not working as expected. Please help me, thanks in advance.
String titleWhenLoad = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
String titleNext = titleWhenLoad;
while ((titleWhenLoad.equals(titleNext))
&& (driver.findElement(By.xpath("//span[#class='assignment_title']")).isDisplayed())) {
WebElement btn = driver.findElement(By.xpath("//button[contains(text(),'Submit')]"))
btn.click();
if (driver.findElement(By.xpath("//span[#class='assignment_title']")).isDisplayed()) {
titleNext = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
} else {
break;
}
}
You can use WebDriverWait utilizing .invisibilityOfElementWithText to wait a element with specific text disappear.
To click the button target, I suggest to use Actions class.
Check it out:
String titleWhenLoad = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
boolean findNewTitle = false;
while (!findNewTitle) {
try {
new WebDriverWait(driver, 3).until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//span[#class='assignment_title']"), titleWhenLoad));
findNewTitle = true;
} catch (Exception e) {
WebElement btn = driver.findElement(By.xpath("//button[contains(text(),'Submit')]"));
Actions builder = new Actions(driver);
builder.moveToElement(btn).click(btn).build().perform();
}
}
Import the following:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.interactions.Actions;
Reference:
https://www.selenium.dev/documentation/en/webdriver/waits/#expected-conditions
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
I made a loop for an iframe locator with java code
but its not working. Can anyone see the problem?
invoking the class with:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver,driver.findElement(By.cssSelector("[href*='Search.mvc'][class*='magnify']")));
and use this after:
searchButton.click();
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, WebElement element) {
try {
driver.switchTo().defaultContent();
element.isDisplayed();
} catch (Exception continueFlow) {
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame));
if (element.isDisplayed()) {
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} return element;
}
}
Because i was passing the whole Webelement By cssSelector it was not even going in the loop. So i pass the element like this:
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver, By.cssSelector("[href*='Search.mvc'][class*='magnify']"));
So i changed the code and removed the By.cssSelector in 2 spots and all working fine now:
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, By element) {
driver.switchTo().defaultContent();
try {
if (driver.findElement(element).isDisplayed()) ;
{
System.out.println("Element is displayed on main page");
}
} catch (Exception continueFlow) {
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
System.out.println("going back to main page");
try {
driver.switchTo().frame(frame);
System.out.println("switched to next frame: " + frame);
if (driver.findElement(element).isDisplayed()) {
System.out.println("element is found in frame: " + frame);
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} System.out.println("returned element succesfully");
return driver.findElement(element);
}
}
How to list all the checkboxes available in a webpage by displaying their Visible Text in selenium using Java?
For that website this should do
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type=checkbox]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
if (checkboxes.isEmpty()) {
System.out.println("No Checkbox present in the page");
} else {
for (WebElement checkbox : checkboxes) {
if (checkbox.isDisplayed()) {
String text=(String) js.executeScript("return arguments[0].nextSibling.textContent.trim();", checkbox);
System.out.println(text);
}
}
}