Reputation: 31
Am trying to automate below steps in the website - wwww.snapdeal.com (1) Log into the website (2) Select Mobiles > Smartphones (3) Select a smartphone model (4) proceed to payment (5) At the Payment screen, enter the Credit Card No (6) Select the Card Expiry Date (Month & Year)
Steps 1 to 5 are passed but Step 6 is failing. System not identifying the Dropdown field so could not select.
Have tried below ways but all are failing // METHOD 1 = USING SELECT CLASS - FAILED //DROWDOWN SELECTION METHOD USING SELECT - PROBLEM DROPDOWN VALUES ARE NOT HAVING SELECT TAG SO SELECT CLASS NOT WORKING // METHOD 2 - USING CLICK AND SENDKEYS - FAILED // METHOD 3 - USING ACTION CLASS - FAILED - Error "Element should have been "select" but was "div" is shown.
Please help.
below is the code and also attached link for the html webelement properties of the field "MM"
package basic;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.sql.Driver;
import java.util.Set;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.interactions.*;
import org.openqa.selenium.Alert;
public class SnapDeal{
public static void main(String[]args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\hp\\workspace\\Selenium\\browser\\
chromedriver.exe");
WebDriver Snap = new ChromeDriver();
Snap.get("https://www.snapdeal.com/"); //log into - www.snapdeal.com
Thread.sleep(3000);
Snap.manage().window().maximize(); // maximze the window
Thread.sleep(2000);
Snap.findElement(By.xpath(".//*[@id='sdHeader']/div[4]/div[2]/div/div[3]/div[3]
/div/span[1]")).click(); // click on sign-in
Thread.sleep(1000);
Snap.findElement(By.xpath("//a [@href='https://www.snapdeal.com/login']")).click(); // click on sign-in
Thread.sleep(1000);
Snap.switchTo().frame("loginIframe"); // switch to child pop up window
Snap.findElement(By.id("userName")).sendKeys("9841490130"); // enter the mobile no
Snap.findElement(By.id("checkUser")).click(); //click proceed
Thread.sleep(1000);
Snap.findElement(By.xpath(".//*[@id='j_password_login_uc']")).sendKeys("badri1984"); // enter the password
Snap.findElement(By.id("submitLoginUC")).click();
Thread.sleep(3000);
Snap.findElement(By.xpath(".//*[@id='leftNavMenuRevamp']/div[1]/div[2]/ul/li[2]/a/span[2]")).click(); // click on mobiles
Snap.findElement(By.xpath(".//*[@id='category2Data']/div[1]/div/div/p[2]/a/span")).click(); // click smartphones
JavascriptExecutor AK = (JavascriptExecutor)Snap;// import JavascriptExecutor interface
AK.executeScript("scroll(0,700)"); //scroll down the window
Snap.findElement(By.xpath(".//*[@id='662623821571']/div[3]/div[1]/a/p")).click();//click on a model, new child tab opened.
String Parwindow = Snap.getWindowHandle(); // capture the parent window id as string "Parwindow"
System.out.println("PARENT WINDOW ID ID " + Parwindow); // Print the Parent Window ID
Set<String> allwindows = Snap.getWindowHandles(); // Capture all the window ids as String allwindows
int count = allwindows.size(); // capture the count of windows ids as int count
for(String Child : allwindows){ // applying "FOR EACH" loop, declaring String Child equals all windows(all windows ids)
if (!Parwindow.equalsIgnoreCase(Child)){ // applying if condition as if parent window id not equals to windows ids captured in String child)
Snap.switchTo().window(Child); // switching to child window using the child window id
System.out.println("CHILD WINDOW IS " + Child); // printing the child window id
Thread.sleep(2000);
JavascriptExecutor SK = (JavascriptExecutor)Snap;// import JavascriptExecutor interface
SK.executeScript("scroll(0,400)");//scroll down the window
Thread.sleep(1000);
Snap.findElement(By.id("buy-button-id")).click();// click on buy bottom
Thread.sleep(2000);
JavascriptExecutor ABN = (JavascriptExecutor)Snap;// import JavascriptExecutor interface
ABN.executeScript("scroll(0,1000)");// Scroll Down
Thread.sleep(1000);
Snap.findElement(By.id("make-payment")).click();// click on payment
Thread.sleep(1000);
JavascriptExecutor NY = (JavascriptExecutor)Snap;// import JavascriptExecutor interface
NY.executeScript("scroll(0,800)");// Scroll down and click on buy bottom
Thread.sleep(1000);
Snap.findElement(By.name("card_number_mask")).sendKeys("4176092389357621"); // enter the card no
// METHOD 1 = USING SELECT CLASS - FAILED
//DROWDOWN SELECTION METHOD USING SELECT - PROBLEM DROPDOWN VALUES ARE NOT HAVING SELECT TAG SO SELECT CLASS NOT WORKING.HOW TO SELECT THE DROPDOWN?
/*WebElement Date = Snap.findElement(By.id("CS2"));
Select Date_D = new Select(Date);
WebElement Date_Value = Date_D.getFirstSelectedOption();
System.out.println("Selected Value for Date is " + Date_Value.getText());
Date_D.selectByIndex(9);*/
// METHOD 2 - USING CLICK AND SENDKEYS - FAILED
//Snap.findElement(By.id("CS2")).click();
//Snap.findElement(By.id("CS2")).sendKeys("09");
// METHOD 3 - USING ACTION CLASS - FAILED - "Element should have been "select" but was "div"
/*Actions act = new Actions(Snap);
act.moveToElement(Snap.findElement(By.id("CS2")));
act.click();
act.sendKeys("09");
WebElement Month = Snap.findElement(By.id("CS3"));
Select Month_MM = new Select(Month);
Month_MM.selectByValue("17");
Thread.sleep(2000);
Month_MM.selectByVisibleText("YY");
Thread.sleep(2000);
Month_MM.selectByVisibleText("19");
Thread.sleep(2000);
Snap.findElement(By.name("cvvValidate")).sendKeys("007");
Thread.sleep(1000);
WebElement Pay_Button = Snap.findElement(By.id("creditcard-continue"));
Pay_Button.getText();
System.out.println("Pay button text is " + Pay_Button.getText());
}
}}}
enter image description here enter image description here
Upvotes: 0
Views: 180
Reputation: 2019
The issue is, you have been looking into the select tag but that is actually hidden(look into its class name). If you inspect the dropdown options for mm, it is actually a Angular element and we can't handle this with selenium select syntax. Below steps will help you to resolve this.
Consider the drop down as an normal element similar to button and click on it, so that the options will appear. This is the way the angular works.
driver.findElement(by.id("CS5")).click();
Now the options such as months will appear and click on it.
//am selecting 05
driver.findElement(by.xpath("//li[contains(text(),'05')]").click();
Note that you might need to use some wait between the step 1 and 2. We can use explicit wait for visible.
Hope this helps thanks.
Upvotes: 0