我试图通过selenium webdriver自动化沃尔玛网站,网址为https://www.walmart.com/lists/create-wedding-registry。我的问题是如何在java中编写日期选择代码 enter image description here
答案 0 :(得分:0)
对于WebDriver来说,这应该是一个相当简单的场景 - 粗略地说,您希望按照以下方式执行某些操作:
// First find the button that makes the date-picker element appear and click it.
driver.findElement(By.id("event-date-picker-input")).click();
// Then get hold of all the available dates.
List<WebElement> dateList = driver.findElement(By.className("dp_daypicker")).findElements(By.tagName("td"));
// Finally, iterate over all the available dates and click the one with the desired date.
for (WebElement date : dateList) {
if (date.getText().equals(whateverDateYouWant)) {
date.click();
break;
}
}
答案 1 :(得分:0)
您可以通过以下方式执行此操作:
接近一个
console.log(this === exports);
console.log(this === module.exports);
方法二
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();
}
}
}
}