This is how you can switch browsers pages using linkText in Selenium as below
public class locatorsPractice {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","/path");
WebDriver driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("https://browser url");
String strMainWindowHandle=driver.getWindowHandle();
System.out.println("Window title"+driver.getTitle());
driver.findElement(By.linkText("Google")).click();
Set <String> strHandles=driver.getWindowHandles();
for (String handle:strHandles) {
driver.switchTo().window(handle);
String strTitle=driver.getTitle();
if(strTitle.equalsIgnoreCase("Google")) {
System.out.println(driver.getTitle());
driver.manage().window().maximize();
Thread.sleep(2000);
driver.close();
}
}
}}
Related
Here is code that I want to do switching. I do some stuff at user X and then again by calling openbrowser() method I login to user y and do some stuff now want to switch to X user chrome browser again for next actions.
public static void openbrowser() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/chromedriver");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
public static void navigateAppUrl(int row, int column) throws Exception {
driver.get(excel.readData(row , column));
}
public static void LogintoXUser() {
//Do some stuff//
}
public static void LogintoYUser() {
//Do some stuff//
}
public static void main(String args[]){
openbrowser();
navigateAppUrl(1,2);
LogintoXUser()
// new chrome browser instance is created
openbrowser();
navigateAppUrl(1,2);
LogintoYUser()
}
Change method openbrowser() to WebDriver. You can create more instances of WebDriver and arbitrarily use them.
public static WebDriver openbrowser() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/chromedriver");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
return driver;
}
public static void main(String args[]){
WebDriver driver1 = openbrowser();
// do some staff with driver1
WebDriver driver2 = openbrowser();
// do some staff with driver2
// continue with some staff with driver1
driver1....
// continue with some staff with driver2
driver2....
}
I created a custom ExpectedCondition to be used as input in my wait.until() method, however when my code reaches my custom ExpectedCondition argument, a NullPointerException is thrown, and I cannot figure out why. I've tried everything, and the same result is always received. Below, you will find my code
CustomWait:
public static ExpectedCondition<Boolean> visibilityOfElement(final
WebElement element) {
return new ExpectedCondition<Boolean>() {
#Override
public Boolean apply(WebDriver input) {
try {
return element.isDisplayed();
}catch(NoSuchElementException e) {
return false;
}catch(StaleElementReferenceException e1) {
return false;
}
}
};
}
}
LoginPage (this page contains the code that calls the CustomWait class method):
public class LoginPage {
WebDriver driver;
WebDriverWait wait;
#FindBy(how=How.ID, using="email") WebElement email;
#FindBy(how=How.ID, using="password") WebElement password;
#FindBy(how=How.ID, using="submit-button") WebElement loginSubmitButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String email, String password) {
wait.until(CustomWait.visibilityOfElement(this.email));
this.email.sendKeys(email);
this.password.sendKeys(password);
loginSubmitButton.click();
}
}
When the program reaches the code "wait.until(CustomWait.visibilityOfElement(this.email))", that is when the NullPointerException is thrown, and I believe that the "WebDriver input" part of my parameter for the visibilityOfElement method of the Custom Wait class is where the problem lies, but I cannot figure out why.
Main(this is where my test is found):
public class Main {
WebDriver driver;
public Main() {
driver = BrowserFactory.startBrowser("chrome",
"http://123help123.com/");
}
#Test
public void smokeTest() {
HomePage homePage = PageFactory.initElements(driver,
HomePage.class);
homePage.clickLogin();
LoginPage loginPage = PageFactory.initElements(driver,
LoginPage.class);
loginPage.login("haha", "123");
}
}
BrowserFactory (this is how my driver is created):
public class BrowserFactory {
static WebDriver driver;
public static WebDriver startBrowser(String browser, String url) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")) {
if(SystemUtils.IS_OS_MAC_OSX) {
System.setProperty("webdriver.chrome.driver",
"src/chromedriver");
}
else if(SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.chrome.driver",
"src/chromedriver.exe");
}
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("ie")) {
driver = new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
Any help is greatly appreciated, and if you need more information, then please let me know.
You got NullPointerException because you haven't initialized wait in your LoginPage class. So there is not driver to pass to your custom ExpectedCondition.
public LoginPage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver,5);
}
This is the code i have tried to handle window but the url for opens in the tab of google.
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver =new ChromeDriver();
driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
try {
Thread.sleep(3000);
for(String windowHandle:driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}
In the iteration over the window handles you are switching to both of them. The last switch returns the focus to the first window and the link is getting opened there.
You should do the switch only to the new window
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
String firstWindowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
try {
Thread.sleep(3000);
for(String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(firstWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}
Driver is opening in same window, because for loop is switching old handle
Code probably should look like this below
System.setProperty("webdriver.chrome.driver", "/home/ish/chromedriver");
WebDriver driver =new ChromeDriver();
driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"n");
String currentHandle = driver.getWindowHandle();
try {
Thread.sleep(3000);
for(String windowHandle:driver.getWindowHandles()) {
if(!currentHandle.equals(windowHandle)){
driver.switchTo().window(windowHandle);
break;
}
}
driver.get("http://fb.com");
} catch (Exception e) {
System.out.println(e);
}
Hi I need to call a method "loop()" 500 times. Do I need to write "loop();" 500 times or is there any method to call it multiple times. Please help with this. The following code is in java and I am doing this with selenium webdriver.
public class Salesforce_login {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:/Users/Master/Desktop/chromedriver.exe");
driver = new ChromeDriver();
// driver = new FirefoxDriver();
Thread.sleep(1000);
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://login.com");
driver.findElement(
By.xpath(".//*[#id='loginSwitcher:loginSwitcherForm']/div[1]/div[1]/div/a"))
.click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//*[#id='username']")).sendKeys(
"*****");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys(
"*****");
driver.findElement(By.xpath(".//*[#id='Login']")).click();
Thread.sleep(30000);
driver.findElement(By.xpath(".//*[#id='moreGroupMembersLink']"))
.click();
Thread.sleep(1000);
loop();
loop();
loop();
loop();
}
public static void loop() throws InterruptedException{
for (int i = 1; i < 25; i++) {
System.out.println(driver
.findElement(
By.xpath(".//*[#id='groupMembersDialogContent']/div/div[1]/div[2]/div/table/tbody/tr["+i+"]/td[2]/div/a"))
.getAttribute("href"));
}
driver.findElement(By.xpath(".//*[#id='groupMembersDialogContent']/div/div[1]/div[3]/div/span[2]/span[1]/a")).click();
Thread.sleep(2000);
}
}
for(int i = 0; i < 500; i++) {
loop();
}
Can any one pls help me out
There are many check boxes available with values and I have to choose a particular value in check box. I dont know how to choose a check box value in selenium webdriver
https://www.blueshieldca.com/fap/app/search.html
This one works.. This will click the check box "General Medicine" under "Type" on left menu...
public class SampleUITest extends SeleneseTestBase {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
try {
driver.get("https://www.blueshieldca.com/fap/app/search.html");
driver.findElement(By.id("location"))
.sendKeys("Locans, Fresno, CA");
driver.findElement(By.className("findNow")).click();
Thread.sleep(1000);
driver.findElement(By.className("continueBtn")).click();
Thread.sleep(15000);
driver.findElement(
By.xpath("//ul[#id='doctortypesmodule']/li[2]/input"))
.click();
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
This works!!
Your actual issue is with wait
#BeforeTest
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.blueshieldca.com/fap/app";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
#Test
public void Test01() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 60);
driver.get(baseUrl + "/search.html");
driver.findElement(By.xpath("//input[#name='location']")).clear();
driver.findElement(By.xpath("//input[#name='location']")).sendKeys(
"Los Alamitos, Orange, CA");
driver.findElement(By.xpath("//input[#value='findNow']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#onclick='continueallPlans();']"))
.click();
wait.until(ExpectedConditions.elementToBeClickable(By
.xpath("//input[#onclick='javascript:results_OnProviderCompareClicked(this);']")));
List<WebElement> ele = driver
.findElements(By
.xpath("//input[#onclick='javascript:results_OnProviderCompareClicked(this);']"));
System.out.println(ele.size());
ele.get(0).click();
}
Prashanth Sams | seleniumworks.com