New Tab is not opening in Selenium - selenium

New Tab is not opening in Selenium
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element=driver.findElement(By.linkText("Gmail"));
Thread.sleep(3000);
element.sendKeys(Keys.CONTROL,"t");
}
}
Please help me find the error

Here is working code..it opens Gmail into new tab and switch to it.
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Gmail")).sendKeys(selectLinkOpeninNewTab);
Thread.sleep(3000);
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
}

To open a New Tab with the linkText("Gmail") you can use the following code block :
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();

open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("http://www.google.com/")).sendKeys(selectLinkOpeninNewTab);

You can use JavaScriptExecutor to do it
Javascriptexecutor js = (Javascriptexecutor)driver;
js.executescript("var win = window.open('"+ YourURLHere + "', '_blank');win.focus(); ");
Above code will open new tab and navigate to the URL provided and focus on the new tab.

Related

Unable to open New tab in new its replacing the first tab rather than opening the new one in Firefox

public class TestTabs {
public WebDriver driver;
public WebDriver getDriver() {
return driver;
}
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver","/Users/Test/Downloads/geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND+ "t");
}
#Test
public void openSameUrlInMultipleTab() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://seleniumlearn.com/selenium-tutorial");
{
driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND+ "t");
driver.get("https://www.facebook.com");
}
}
}
I am using the above code to open multiple tabs in one browser but When I am running this code in Firefox (iMac mini) I am not getting any error message and code is passed but 2nd link is opening in same tab rather than in new tab. How can I open multiple tabs in Firefox in Mac?
Each tab is considered as a new window. switch to the new tab using the switch.to().window().
driver.switchTo().window(winHandle);
driver.get("your new url goes here");
User below line if you want to switch to default browser (the base browser).
driver.switchTo().defaultContent();

How do I select package using selenium webdriver

public class chrome {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://neontv.co.nz");
Thread.sleep(2000);
driver.findElement(
By.xpath("//*[#class='nv-nav-freetrial-2 nv-nav-item']"))
.click();
Thread.sleep(200);
driver.findElement(
By.xpath("//ul[#id='yui_patched_v3_11_0_2_1529643447183_22']"))
.click();
Thread.sleep(2000);
driver.quit();
}
}
I am unable to select package on next page.. I tried changing the xpath but it didnt help.. is there any other way to select this. Could any one help please
Using Action classes, I was able to resolve this
driver.switchTo().defaultContent();
Actions action = new Actions(driver);
action.moveToElement(btnEnjoy).click().build().perform();

Google search locate elements not working [duplicate]

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewGmail {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.findElement(By.id("identifierId")).sendKeys("cp8805");
//driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#class='whsOnd zHQkBf']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
}
}
after mail id my password also get written in the id box option & the server redirect to to next password page. i want to ask what i will do so that my password would be entered only in password page.
Here is the working code block to login into your Gmail account through a valid set of credentials-
System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[#id='identifierId']"));
email_phone.sendKeys("your_email_phone");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[#name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
driver.findElement(By.id("passwordNext")).click();
Update(5-Jan-2020)
Optimizing the above code block and adding a couple of arguments you can use:
public class browserAppDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
WebDriver driver = new ChromeDriver(options);
driver.get("https://accounts.google.com/signin")
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='identifierId']"))).sendKeys("emailID");
driver.findElement(By.id("identifierNext")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='password']"))).sendKeys("password");
driver.findElement(By.id("passwordNext")).click();
}
}

Unable to click on login button of paytm. It will highlight but will not click. Below is my code

I am unable to click on login button of paytm. It will highlight but will not click.
The code works perfectly in Firefox. It's not able to click the link in Chrome browser. It's not showing any error or exception, it's just not able to click.
Below is my code:
driver.get("https://paytm.com/");
WebElement LoginLink= driver.findElement(By.xpath("//*[contains(text(),'Log In/Sign Up')]"));
Highlight.highLightElement(driver, LoginLink);
LoginLink.click();
public class LoginPage {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
String url="https://paytm.com/";
driver.get(url);
Thread.sleep(5000);
WebElement Login=(WebElement) driver.findElement(By.className("_3ac-"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", Login);
}
}
driver.manage().window().maximize();
driver.get("https://paytm.com/");
Thread.sleep(2000);
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Login_Btn= driver.findElement(By.xpath("//*[contains(text(),'Log In/Sign Up')]"));
je.executeScript("arguments[0].scrollIntoView(true);",Login_Btn);
Login_Btn.click();
Here is the Answer to your Question:
You can use the following code block to click on Log In/Sign Up button on https://paytm.com/ through Google Chrome 59.0:
Note: As a quick fix to your issue I have induced Thread.sleep(5000); which should be replaced by ExplicitWait i.e WebDriverWait
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Q45102095_PAYTM
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://paytm.com/");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Thread.sleep(5000);
WebElement login_button = driver.findElement(By.xpath("//div[#id='app']//div[normalize-space(text()) = 'Log In/Sign Up']"));
login_button.click();
}
}
Let me know if this Answers your Question.

Unable to enter any data in Google page through Selenium

I am able to open a google page, but I am unable to enter any text in the Search bar. My execution stops only after opening the Google page.
Below is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirePath {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver Driver = new FirefoxDriver();
Driver.get("http://www.google.com");
//Driver.wait(50);
Driver.findElement(By.xpath(".//*[#id='gs_htif0']']")).sendKeys("some text");
//Driver.findElement(By.cssSelector(".gb_P")).click();
//Driver.findElement(By.xpath("html/body/nav/div/a[1]")).click();
}
}
try the following code, if you want to enter text in Search input field:
Driver.findElement(By.xpath("//input[#id='lst-ib']")).sendKeys("some text");
or
Driver.findElement(By.id("lst-ib")).sendKeys("some text");
You can use following code for this.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
driver.findElement(By.id("lst-ib")).sendKeys("Some Text");
driver.findElement(By.id("_fZl")).click();