硒网络驱动程序中用于选择日期的完整代码 [英] Complete code for picking date in selenium webdriver
本文介绍了硒网络驱动程序中用于选择日期的完整代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过硒Webdriver,URL使Walmart网站自动化- https://www.walmart.com/lists/create-wedding-registry .我的问题是如何在Java中编码日期选择 在此处输入图像描述
I'm trying to automate Walmart site through selenium webdriver, url - https://www.walmart.com/lists/create-wedding-registry. My question is how to code date picking in java enter image description here
推荐答案
您可以通过以下方式进行操作:
You can do this in following ways :
方法一
import java.util.List;
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;
public class WalmartDatePicker {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.getProperty("webdriver.chrome.driver","C:\\Users\\rajnish\\Desktop\\ImpFolder\\SeleniumJar\\chromedriver_win32 (1)");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.walmart.com/lists/create-wedding-registry");
// first click inside the Event date it will make date picker to appear
driver.findElement(By.id("event-date-picker-input")).click();
// how to select next month from the date picker
// selecting month
driver.findElement(By.xpath("//*[@class='dp_next']")).click(); // will move to april one more click to may and so on
// now how to select a specific date form the calendar
// prints complete content of the calendAr
// plz think calendar as a web table with a column and row
// hence there are 7 tr in source so select an value make ur spath like below
// for row1 = xpath will be like Xpath = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[1]/th"))
List<WebElement> completecalContent = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[1]/th"));
for(int i = 0;i<completecalContent.size();i++){
System.out.println("contents for 1st tr is : " + completecalContent.get(i).getText());
}
// for row2 = xpath will be like Xpath = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[2]/td"))
completecalContent = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[2]/td"));
for(int i = 0;i<completecalContent.size();i++){
System.out.println("contents for 2nd tr is : " + completecalContent.get(i).getText());
}
// for row3 = xpath will be like Xpath = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[3]/td")) and so on
completecalContent = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[3]/td"));
for(int i = 0;i<completecalContent.size();i++){
System.out.println("contents for 3rd tr is : " + completecalContent.get(i).getText());
}
// now selecting a specific date .suppose want to select future date say 28 march 2016.
// u can say that by seeing tr it lies in the 6th tr in source code hence xpath will be
// for row6 = xpath will be like Xpath = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[6]/td[1]")) here td is one as 28 is first td
completecalContent = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr[6]/td[1]"));
for(int i = 0;i<completecalContent.size();i++){
System.out.println("contents for 6th tr is : " + completecalContent.get(i).getText());
// to select simply use
if(completecalContent.get(i).getText().equals("28")){
completecalContent.get(i).click();
}
}
}
}
方法二
import java.util.List;
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;
public class WalmartDatePicker {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.getProperty("webdriver.chrome.driver","C:\\Users\\rajnish\\Desktop\\ImpFolder\\SeleniumJar\\chromedriver_win32 (1)");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.walmart.com/lists/create-wedding-registry");
// first click inside the Event date it will make date picker to appear
driver.findElement(By.id("event-date-picker-input")).click();
// now how to select a specific date form the calendar
// selecting month
driver.findElement(By.xpath("//*[@class='dp_next']")).click(); // will move to april one more click to may and so on
List<WebElement> completecalContent = driver.findElements(By.xpath("//*[@class='dp_daypicker']/tbody/tr/td"));
for(int i = 0;i<completecalContent.size();i++){
System.out.println("Print complete Content : " + completecalContent.get(i).getText());
if(completecalContent.get(i).getText().equals("28")){
completecalContent.get(i).click();
}
}
}
}
这篇关于硒网络驱动程序中用于选择日期的完整代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文