如何在日历中搜索启用元素并单击它?

时间:2016-05-25 11:32:57

标签: selenium-webdriver

我需要知道点击日历中活动元素(日期)的代码以及航空公司页面的活动单选按钮。我该怎么做?请帮忙..

我已经厌倦了一个特定的日期,但过了一段时间后它无法点击该元素..错误 - 无法找到元素......

这是代码: -

public class Oneway 

{
    public static void main (String[] args) throws InterruptedException { 

System.setProperty("webdriver.chrome.driver","D:\\elephant\\chromedriverw.exe");
{

//Launch crome n maximise window
WebDriver driver=new ChromeDriver();
driver.get("http://www.vueling.com/es");
driver.manage().window().maximize();

//selecting dropdowm origin n destination

driver.findElement(By.name("AvailabilitySearchInputXmlSearchView$TextBoxMarketOrigin1")).sendKeys("Barcelona");

Actions a=new Actions(driver);
    a.sendKeys(Keys.DOWN);
    a.build().perform();
     Thread.sleep(2000); 

a.sendKeys(Keys.ENTER).build().perform();
    Thread.sleep(2000);

    driver.findElement(By.name("AvailabilitySearchInputXmlSearchView$TextBoxMarketDestination1")).sendKeys("Madrid");


Actions a1=new Actions(driver);
for (int i=1; i<=1; i++)
a1.sendKeys(Keys.DOWN);a.build().perform();
Thread.sleep(2000);

a.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);


// Date Picker

driver.findElement(By.id("divBuscadorCalendarTarget"));
driver.findElement(By.cssSelector("#datePickerContainer > div.ui-datepicker-group.ui-datepicker-group-first > table > tbody > tr:nth-child(5) > td:nth-child(4) > a")).click();
   Thread.sleep(5000);


   driver.findElement(By.cssSelector("#datePickerContainer > div.ui-datepicker-group.ui-datepicker-group-last > table > tbody > tr:nth-child(1) > td:nth-child(3) > a")).click();


   driver.findElement(By.xpath("/HTML[1]/BODY[1]/DIV[2]/DIV[2]/DIV[1]/DIV[1]/DIV[1]/DIV[1]/FORM[1]/DIV[1]/DIV[1]/DIV[8]/DIV[1]/DIV[1]/A[1]")).click();

1 个答案:

答案 0 :(得分:0)

试试这个。这可以单击日历的值。

WebDriver driver = new FirefoxDriver();

适用于Chrome驱动程序

你需要exe来运行chromedriver,这是

http://chromedriver.storage.googleapis.com/index.html?path=2.21/ 只需提取它并给出它的跟踪路径。

System.setProperty("webdriver.chrome.driver", "path where the exe is");

例如

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");


WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("http://www.vueling.com/es#");
Thread.sleep(4000L);

WebElement  element  = driver.findElement(By.xpath(".//*[@id='marketDate1_lab']"));
element.click();
Actions a = new Actions(driver);
WebElement b = driver.findElement(By.xpath(".//*[@id='datePickerContainer']/div[1]/table/tbody/tr[5]/td[4]/a"));
a.moveToElement(b).build().perform();
b.click();

WebElement c =driver.findElement(By.xpath(".//*[@id='datePickerContainer']/div[1]/table/tbody/tr[5]/td[4]/a"));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='datePickerContainer']/div[1]/table/tbody/tr[5]/td[4]/a")));
c.click();

回复我以进一步查询。快乐学习: - )