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();
Related
The following is my code. One browser opens blank and another opens google.
I am using the latest version of chrome driver. Thanks in advance.
public class Trials {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
Thread.sleep(3000);
driver.close();
}
}
I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod
As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();
This question already has an answer here:
Gmail login using selenium webdriver in java
(1 answer)
Closed 3 years ago.
I am unable to log in to Gmail using my code. Please help
public class first_test {
WebDriver driver = new ChromeDriver();
#BeforeTest
public void tearup()
{
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://accounts.google.com");
}
#Test
public void mail() throws InterruptedException {
WebElement email=driver.findElement(By.id("identifierId"));
email.click();
email.sendKeys("sharfulumair");
WebElement password=driver.findElement(By.id("//*[#name=\"password\"]"));
password.click();
password.sendKeys("abcd");
}
#AfterTest
public void teardown()
{
driver.close();
}
}
The error message I got is:
Unable to locate element: {"method":"id","selector":"identifierId"}
Try using wait before accessing the webelement.
public void mail() throws InterruptedException {
new WebDriverWait(driver, 60).until(ExpectedConditions.presenceOfElementLocated(By.id("identifierId")));
WebElement email=driver.findElement(By.id("identifierId"));
...............
}
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.
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.