I tried this exact code on other websites and it seems to work fine. It's just on pizza hut that it can't even locate an element let alone click on it. Thread.sleep() doesn't make a difference. The problem is between the commented *, according to the compiler. Here's the code.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
//************************************************
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
I just ran the code below and it worked just fine. No need for sleep or waits and I'm using Chrome also.
driver.get("https://www.pizzahut.com");
driver.findElement(By.id("lg-nav-pizza")).click();
Try this one ,definitely it help you.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
// System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
List<WebElement> webElements=driver.findElement(By.cssSelector("div.btn-group.btn-group-lg.btn-group-justified.ph-header-navigation"))
.findElements(By.cssSelector("div.btn.btn-link.ph-ghost-padding.ng-scope"));
for (WebElement element : webElements) {
if (element.getAttribute("title").equals("PIZZA")) {
element.click();
}
}
Thread.sleep(5000);
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
You are not able to click it because it is an AngularJS tag, hence it takes some time to load: add some wait like below and it works (Here i'm just adding thread.sleep for the sake of simplicity,I've increased it to 15000ms):
//CLICK PIZZA
Thread.sleep(15000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
Related
.clear() in my test script is not working with testng version 6.14.2 but when i am running the same code without testng the clear method is working as expected.
i am running the code as mentioned below:
driver.findElement(By.id("email")).clear();
But this loc is not performing any action.
Blockquote
clear() is working for me. refer the following snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Testngexample {
private WebDriver driver;
#BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver","Add chromedriver path chromedriver.exe");
driver = new ChromeDriver();
}
#AfterClass
public void tearDown() {
driver.quit();
}
#Test
public void GoogleEarch() throws InterruptedException {
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).click();
driver.findElement(By.name("q")).sendKeys("testing");
Thread.sleep(5000);
driver.findElement(By.name("q")).clear();
Thread.sleep(5000);
driver.close();
}
}
So i have the following java code with a selenium webdrive code. The code works as intended untill the AddItems function. It does not work, I can't make it continue the work from the Login function. I've tried calling both function in the main, i've tried calling one AddItems into Login. I don't understand how i should link the two process so one continuies the other. I've tried what i've seen here: https://www.youtube.com/watch?v=ph3NJm4Z7m4&t=4472s , at 1:02:44 ish .
Please help me understand how can the function use the same "test" and continue the function.
package TestEmagShoppingCart;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ShoppingCart {
WebDriver test;
public void Login() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver test = new ChromeDriver();
test.get("http://www.emag.ro");
test.manage().window().maximize();
//test.manage().deleteAllCookies();
//test.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//test.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
String title = test.getTitle();
System.out.println("Titlul paginii este: "+ title);
test.findElement(By.xpath("/html/body/div[3]/nav[1]/div/div/div[3]/div/div[2]/a/span")).click();
test.findElement(By.id("email")).sendKeys("anghelalex1994#gmail.com");
Thread.sleep(1000);
test.findElement(By.xpath("/html/body/form/div[4]/div/button")).click();
Thread.sleep(1000);
test.findElement(By.id("password")).sendKeys("alex21");
test.findElement(By.xpath("/html/body/form/div[4]/div/button")).click();
//test.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[1]/div[1]/ul[1]/li[5]/a[1]")).click();
//AddItems();
}
public void AddItems()
{
test.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[1]/div[1]/ul[1]/li[5]/a[1]")).click();
}
public static void main(String[] args) throws InterruptedException {
ShoppingCart cart = new ShoppingCart();
cart.Login();
cart.AddItems();
}
}
Please use PageObject and add all action listener from there:
public class EmagPageObject {
private WebDriver driver;
public EmagPageObject(WebDriver driver) {
this.driver = driver;
}
public EmagPageObject loginToApp(String user, String password) {
// Your code
return this;
}
public EmagPageObject AddItems() {
// Your code
return this;
}
}
And do not user thread.sleep use only Implicit Wait or Explicit Waits
I've fixed it myself.
I've deleted " WebDriver test = new ChromeDriver();" from the Login function and put it as a global variabile exactly as written above.
Till yesterday the below mentioned code was working fine but now i am facing some problem .
The code opens firefox browser then loads facebook.com but the code is not sending
email and password to web browser i.e. sendkeys() is not working.
I verified the id of both textbox of email and password which are correct yet code is not working .
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class Webdriver2 {
WebDriver driver ;
JavascriptExecutor jse;
public void invokeBrowser()
{
try
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
driver.get("https://www.facebook.com/");
search();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void search()
{
try
{
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
Thread.sleep(4000);
driver.findElement(By.id("pass")).sendKeys("password");
Thread.sleep(4000);
driver.findElement(By.id("u_0_2")).click();
Thread.sleep(4000);
/*driver.findElement(By.name("q")).sendKeys("spit mumbai");
Thread.sleep(4000);
driver.findElement(By.xpath(" //button[#aria-label='Search' and #data-testid='facebar_search_button'] ")).click();*/
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Webdriver2 w = new Webdriver2();
w.invokeBrowser();
}
}
Try clicking on the two textboxes before you are performing the sendKeys:
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
The textboxes probably needs focus.
We need to take care of a couple of things here as follows:
The Email or Phone field is within an <input> tag so we need to take it into account while selecting the locator.
The Password field is also within an <input> tag so we need to take it into account while selecting the locator.
If you observe closely the id of the Log In button is dynamic and also within an <input> tag, so we need to consider this factor as well.
Here is the minimum sample code block using cssSelector to access the url https://www.facebook.com/, provide Email or Phone and Password, finally click on Log In button:
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Facebook_Login_CSS_FF {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Ryusei");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Nakamura");
driver.findElement(By.cssSelector("input[value='Log In']")).click();
}
}
I am using selenium and testNG framework for my project.
Now what is happening is each class is opening up a browser and then run its methods, eg, if I have five classes, then five browsers will open simultaneously and then run the tests.
I want to Open Browser at the start once and run all the methods and then close it.
public class openSite
{
public static WebDriver driver;
#test
public void openMain()
{
System.setProperty("webdriver.chrome.driver","E:/drive/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://vtu.ac.in/");
}
#test
//Clicking on the first link on the page
public void aboutVTU()
{
driver.findElement(By.id("menu-item-323")).click();
}
#Test
//clicking on the 2nd link in the page
public void Institutes()
{
driver.findElement(By.id("menu-item-325")).click();
}
Now What I want is the testNG should open browser once and open vtu.ac.in once and then execute the methods aboutVTU and Institutes and give me the result
You already declared the type for driver in your field declarations. Redeclaring it in openMain() is your problem. It should look like this.
import static org.testng.Assert.assertNotNull;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class OpenSite {
private WebDriver driver;
#BeforeClass(alwaysRun=true)
public void openMain()
{
System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");
driver = new ChromeDriver();
driver.get("http://vtu.ac.in/");
}
#Test
//Clicking on the first link on the page
public void aboutVTU()
{
assertNotNull(driver);
driver.findElement(By.id("menu-item-323")).click();
}
#Test(dependsOnMethods="aboutVTU")
//clicking on the 2nd link in the page
public void Institutes()
{
assertNotNull(driver);
driver.findElement(By.id("menu-item-325")).click();
}
}
I want to be able to scroll the subscriptions list of my personal YouTube page, how do I do this? I have written code that allows me to scroll the main page, any ideas as to how the code could be tweaked to scroll the "My subscriptions" section of the YouTube page that appears when signed into YouTube?
package Check;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class java {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
Thread.sleep(2500);
driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(1500);
driver.findElementById("Email").sendKeys("<My username>");
driver.findElementById("Passwd").sendKeys("<My password>");
driver.findElementById("signIn").click();
//driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(3500);
driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
driver.findElementById("identity-prompt-confirm-button").click();
Thread.sleep(2500);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
Thread.sleep(5000);
driver.quit();
}
}
I have found by a method of trial and error a method that works, by the CSS selector appears to be limited to finding up to the 35th sibling so in the light of this limitation here is the code that I came up with this works very well for what I want to accomplish. Here is my script below:
package Check;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;
public class java2 {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
Thread.sleep(2500);
driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(1500);
driver.findElementById("Email").sendKeys("<User name>");
driver.findElementById("Passwd").sendKeys("<Password>");
driver.findElementById("signIn").click();
Thread.sleep(4000);
driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
driver.findElementById("identity-prompt-confirm-button").click();
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor)driver;
int i =0;
String CSSText = "ul[id='guide-channels'] > li";
do {
if (driver.findElementByCssSelector(CSSText).getText().equals("Josie Outlaw")){
break;
}
CSSText = CSSText + " + li";
i++;
} while (i<35);
js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(false);");
Thread.sleep(1500);
js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(true);");
Thread.sleep(10000);
driver.quit();
}
}