Selenium webdriver selenium webdriver中拾取日期的完整代码

Selenium webdriver selenium webdriver中拾取日期的完整代码,selenium-webdriver,Selenium Webdriver,我正试图通过SeleniumWebDriver,url-自动化沃尔玛网站。我的问题是如何用java编写日期选择代码 对于WebDriver来说,这应该是一个相当简单的场景-大致上,您需要按照以下思路做一些事情: // First find the button that makes the date-picker element appear and click it. driver.findElement(By.id("event-date-picker-input")).click();

我正试图通过SeleniumWebDriver,url-自动化沃尔玛网站。我的问题是如何用java编写日期选择代码

对于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;
   }
}

您可以通过以下方式执行此操作:

接近一号

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();
            }
        }

    }
}

你好,Raj,非常感谢您宝贵的回复,我尝试了List date=d.findElementsBy.xpath/*[@class='dp_daypicker']/tbody/tr/td;forint i=0;这很简单,请看最新的答案。如果这是你想要的解决方案,请将其标记为你问题的答案。非常感谢Raj。。这实际上帮助了我。。谢谢您的欢迎,请将其标记为您问题的答案