【问题标题】:Java selenium - Can't select date from calendar popupJava selenium - 无法从日历弹出窗口中选择日期
【发布时间】:2019-08-08 15:11:10
【问题描述】:

我是使用 selenium webdriver 进行自动化的新手。我正在尝试在旅游网站上预订航班。 (ryanair.com)

我遇到了弹出日期选择器。我只能使用 .sendkeys 输入日期,在单击日期输入上的任何字段后,它会触发弹出日历出现,所以如果输入的格式是 dd/mm/yyyy 并且我想输入“20042019”它只输入20 在 dd 然后自动选择当前月份和年份作为自动完成并打开日历弹出窗口。

我读过一些文章说这些日历通常有两种类型 1. iframe 2. datepicker - 我认为 ryanair 上的那个是基于下面 xpath 的 datepicker

//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month')

也许这是错误的 xpath?但我认为这是正确的

我已尝试使用以下方法查找要预订的日期列表:

   List<WebElement> list_AllDateToBook = driver.findElements(By.xpath()
   System.out.println("size of list is : " + list_AllDateToBook.size() );
   System.out.println("list is : " + list_AllDateToBook );

输出是: 列表大小为:0 列表是:[]

当我使用 xpath 输入日期字段的日期时,它适用于第一个输入:

WebElement day = driver.findElement(By.xpath("//*[@id='row-dates-pax']/div[1]/div/div[1]/div/div[2]/div[2]/div/input[1]"));

但是,当我将 xpath 更改为第二个输入时,它不会输入第二个输入(月)

WebElement day = driver.findElement(By.xpath("//*[@id='row-dates-pax']/div[1]/div/div[1]/div/div[2]/div[2]/div/input[2]"));

下面是一个日期选择器 HTML 示例(添加它太长了!)

   <core-datepicker class="start-date" default-date="" start-date="18-03-2019" end-date="28-03-2020" highlight-from="20-03-2019" highlight-to="20-04-2019" end-show="true" fly-out="true" value="dateRange.startDate" cb-event-id="cal-selector:select-start-date" unavailable-dates="dateRange.unavailabiltyStartDates" selected-month-view="dateRange.startDateSelectedMonthView" show-month-toggle="::dateRange.showMonthToggle" show-single-month=""><!----><div ng-class="::{'has-monthly-toggle': isMonthToggleVisible()}"><div bindonce="" class="datepicker-wrapper r scrollable value-selected" ng-class="{ scrollable: !device.isPhone(), mobile: device.isPhone(), 'value-selected': value, 'six-rows': checkIf6Rows()}" style="transition-timing-function: cubic-bezier(0.1, 0.57, 0.1, 1); transition-duration: 0ms; transform: translate(0px, 0px) translateZ(0px);"><!----><!----><ul ng-if="!device.isPhone()"><!----><li bindonce="" ng-repeat="i in _monthViews" ng-class="{ 'starting-month': checkIfIsSame(getDate(i), highlightFrom, 'month'), 'selected-month': checkIfIsSame(getDate(i), value, 'month'), 'highlight-on': canHighlight(i) }" class="calendar-view starting-month selected-month"><h1 class="month-name">March 2019</h1><ul class="week-days"><!---->

我真的被困在这里了。任何建议都会很棒。

谢谢

【问题讨论】:

  • 请在此处添加您要访问的元素的html
  • 您是否尝试过通过 'data-id' 属性访问日历中的特定日期?说 //*[@data-id='24-03-2019']
  • @SameerArora - html 对于评论框来说太长了
  • @Hairdo 请仅在问题中添加。
  • 您能否在问题中添加一些 HTML,它不必是完整的日期选择器代码,而是您尝试选择的几个日期元素的示例,或者如果这是您正在开发的公共网站,您可以提供链接吗? (您提到了瑞安航空,但我在主页上的日期选择器上找不到与问题匹配的元素)

标签: selenium selenium-webdriver automation calendar


【解决方案1】:

这是来自 python 的工作代码。请参阅我遵循的方法并在 java 中进行模拟。

import os
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def get_full_path_to_folder(folderName):
    folders = os.path.abspath(os.pardir).split(os.sep)
    folderPath = ''
    for folder in folders:
        if folderPath == '':
            folderPath = folder
        else:
            folderPath = folderPath + "\\" +folder
        if os.path.isdir(os.path.join(folderPath, folderName)):
            return os.path.join(folderPath, folderName)
            break
def wait_for_element_present(locator_type, locator):
    if locator_type == 'xpath':
        return (wait.until(EC.presence_of_element_located((By.XPATH, locator))))
    elif locator_type == "css":
        return (wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, locator))))

chrome_options = ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(executable_path=os.path.join(get_full_path_to_folder('drivers'), "chromedriver.exe"))
driver.implicitly_wait(10)
wait = WebDriverWait(driver,10)
url = "https://www.ryanair.com/us/en/"
# go to url
driver.get(url)
#================ you should focus the below code=======================
# close the cookie pop up, otherwise the date and country elements not interable for selenium for interaction
wait_for_element_present('css',".cookie-popup__close").click()
#click on destination edit box
driver.find_element_by_css_selector(".route-selector-destination").click()
# select desitnation country
destiCountry = wait_for_element_present('xpath',"//div[@ng-repeat='option in $ctrl.firstOptions track by option.id' and text()=' Italy']")
destiCountry.click()
#select destination airport
desti = wait_for_element_present('xpath',"//span[contains(@ng-if,'secondOptionsEnabled') and .='Naples']")
desti.click()
# select outbound date
dateEle = wait_for_element_present('css',"li[data-id='24-03-2019']")
if dateEle.value_of_css_property('font-size') == '15px':
    dateEle.click()
#select in bound date
dateEle = wait_for_element_present('css',"li[data-id='20-04-2019']")
if dateEle.value_of_css_property('font-size') == '15px':
    dateEle.click()
#hurry, the date selection is successful.

【讨论】:

  • 感谢您的帮助。不幸的是,这不是问题。字体大小为 15 像素。这些日期有可用的航班,如果我手动将其输入网站并单击下一步,一切都很好。
  • 抱歉,这意味着您可以在两个字段中选择日期,但在单击Let's go 按钮时无法导航到下一页?或者自动选择日期不允许您导航到下一页?
  • 我无法选择任何日期。我似乎根本无法与日期选择器交互。我可以单击 dd ​​或 mm 或 yyyy 字段,这将启用弹出窗口。但在此之后,我根本无法与日历弹出窗口进行交互。或在输入字段中输入任何日期
  • 可以选择目的地吗?
  • 我只能通过发送键选择目的地并输入只有一个可用机场的目的地国家。我无法从机场列表中选择
【解决方案2】:

请尝试:

li[data-id='12-04-2019'] span

注意:添加 until span 标签。刚刚通过控制台尝试并正常工作。

document.querySelector("li[data-id='12-04-2019'] span").click()

【讨论】:

  • 感谢您的帮助。不幸的是,我得到以下信息:无效的选择器:由于以下错误,无法找到具有 xpath 表达式 li[data-id='12-04-2019'] 跨度的元素:SyntaxError: Failed to execute 'evaluate' on 'Document':字符串 'li[data-id='12-04-2019'] span' 不是有效的 XPath 表达式。
  • 我正在使用 WebElement date = driver.findElement(By.xpath("li[data-id='12-04-2019'] span")); date.click();
  • 网站充满了这些弹出式元素,我必须从中进行选择。我不知道如何处理这些类型的元素,但似乎我真的需要!
  • 提供的路径是 CSS 之一。对于 xpath,请使用 driver.findElement(By.xpath("//li[@data-id='12-04-2019']/span"))
  • 没有这样的元素:无法定位元素:{"method":"xpath","selector":"//li[@data-id='12-04-2019']/span "}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多