【发布时间】:2017-07-22 14:10:02
【问题描述】:
我正在尝试自动化网站中的以下步骤 - wwww.snapdeal.com (1) 登录网站 (2) 选择手机 > 智能手机 (3) 选择智能手机型号 (4) 进行付款 (5) 在支付界面,输入信用卡号 (6) 选择卡的有效期(年月)
步骤 1 到 5 已通过,但步骤 6 失败。系统未识别下拉字段,因此无法选择。
尝试了以下方法,但都失败了 // 方法 1 = 使用选择类 - 失败 //使用 SELECT 的下拉选择方法 - 问题下拉值没有选择标签,所以选择类不起作用 // 方法 2 - 使用点击和发送键 - 失败 // 方法 3 - 使用动作类 - 失败 - 错误“元素应该是“选择”,但显示的是“div”。
请帮忙。
下面是“MM”字段的html webelement属性的代码和附加链接
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());
}
}}}
【问题讨论】:
-
我还没有完全看到你的问题,但我认为这可能是一个时间问题。能不能去掉所有thread.sleep,在启动驱动后加上隐式等待?WebDriver driver = new FirefoxDriver();driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
-
@santhosh,感谢您的回复,但已经尝试过隐式等待但仍然失败。问题是硒没有识别“MM”网络元素本身。
-
请告诉我 snapdeal 主页的导航步骤。
-
以下是导航步骤(1)登录www.Snapdeal.com
-
好的..卡支付..到期日..我正在调查。
标签: java eclipse selenium dropdown