【问题标题】:Calendar Element not getting selected- Selenium Webdriver日历元素没有被选中 - Selenium Webdriver
【发布时间】:2015-03-30 08:40:20
【问题描述】:

我正在使用 selenium Webdriver(Java) 学习自动化,我想在 this webpage. 上练习一些东西

我在使用日期选择器选择特定日期时遇到问题。这是我尝试这样做的代码:

String parentWindow = driver.getWindowHandle();
String subWindow = null;
driver.findElement(By.xpath(".//*[@id='ns_7_CO19VHUC6VU280AQ4LUKRK0IR7_fmOutboundDateDisplay']")).click(); //Clicking on datepicker icon

// Change to a new window
String parentWindow = driver.getWindowHandle();
String subWindow = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator1 = handles.iterator();
while (iterator.hasNext()){
    subWindow = iterator.next();
}
driver.switchTo().window(subWindow);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@class='calendarBodyContainer']/tr[2]/td[3]/span")).click(); //Departure Date- 10Feb/2015
driver.findElement(By.xpath(".//*[@class='calendarBodyContainer']/tr[4]/td[4]/span")).click(); //Arrival Dtae- 25 Feb/2015
driver.switchTo().window(parentWindow);

但是,我收到以下错误:

线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素:{"method":"xpath","selector":"//*[@class='calendarBodyContainer']/tr[ 2]/td[3]/span"} 命令持续时间或超时:3.12 秒

请帮忙。

【问题讨论】:

    标签: java selenium xpath selenium-webdriver automated-tests


    【解决方案1】:

    我会尝试两件事:

    • 尝试以下xpath(依赖于table 元素和span 的文本):

      //table[@class='calendarContainer'][1]//span[. = '09']
      
    • explicitly waiting for an element:

      WebDriverWait wait = new WebDriverWait(webDriver, 5);
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='calendarContainer'][1]//span[. = '09']"))).click();
      

    我也不确定这里切换窗口的必要性

    【讨论】:

    • 感谢您的解决方案。但是,它不起作用。有多个日期,与 xpath 匹配
    • @Nik_stack 多个?你的意思是两个,对 - 在左右日历中?无论如何,更新 - 使用[1] 在第一个内部搜索。请检查。
    • :我尝试使用 [1]。但是,如果我尝试评估 firepath 上的命令,它仍然会突出显示两个日期(来自右侧和左侧日历)。无论如何,当我使用 webdriver 执行命令时,它找不到该元素。
    【解决方案2】:

    问题是您将日历小部件误认为是新窗口并相应地自动化,导致找不到元素,@alecxe

    正确怀疑

    请尝试以下代码,看看它是否适合您。

    WebDriver driver=new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    
    //Navigating to the site
    driver.get("http://www.lufthansa.com/online/portal/lh/us/homepage");
    
    //Clicking on the Departing field to select date
    driver.findElement(By.id("ns_7_CO19VHUC6VU280AQ4LUKRK0IR7_fmOutboundDateDisplay")).click();
    
    //Selecting Feb 10, 2015 for departure date 
    driver.findElement(By.xpath("//td[@dojoattachpoint = 'calRightNode']//span[.='10']")).click();
    
    //Waiting for the return calendar with "Return" as the header to appear
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dojoattachpoint='calHeadlineNode' and contains(text(),'Return')]")));
    
    //Selecting Feb 26, 2015 for returning date
    driver.findElement(By.xpath("//td[@dojoattachpoint = 'calLeftNode']//span[.='26']")).click();
    

    注意:我添加了 explicit wait 用于等待“返回日历小部件”中的返回文本,因为它与出发/出站日历重叠,因此 selenium 需要一点时间来检测DOM 的变化。

    【讨论】:

    • 完美解决方案的人。谢谢一堆。我已经意识到问题在于切换窗口,但是;我也很难找到正确的 X 路径。我想出了这样的东西: driver.findElement(By.xpath(".//*[@class='lh_calendarMatrix']/div[.='February 2015']/following-sibling::table/tbody/tr [2]/td[5]/span[.='12']")).click(); //到达日期 -12 Feb/2015
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多