Java Selenium-无法从日历弹出窗口中选择日期

时间:2019-03-18 09:28:22

标签: selenium selenium-webdriver automation calendar

我刚接触硒Webdriver进行自动化。我正在尝试在旅行网站上预订航班。 (ryanair.com)

我被弹出式日期选择器卡住了。我可以使用.sendkeys仅输入日期,单击日期输入中的任何字段后,它将触发弹出日历显示,因此,如果输入格式为dd / mm / yyyy,而我想输入“ 20042019”,则仅输入在dd中输入20,然后自动将当前月份和年份选择为自动完成,并打开日历弹出窗口。

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

import {BackHandler} from 'react-native';

export default class Component extends Component {
     _didFocusSubscription;
     _willBlurSubscription;
     constructor(props) {
         super(props);
         this._didFocusSubscription = props.navigation.addListener('didFocus',payload =>
            BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
         ); 
     }
  }
componentDidMount() {
        this._willBlurSubscription = this.props.navigation.addListener('willBlur', payload =>
            BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
        );
 }
componentWillUnmount() {
        this._didFocusSubscription && this._didFocusSubscription.remove();
        this._willBlurSubscription && this._willBlurSubscription.remove();
    }
onBackButtonPressAndroid = () => {
    //code when you press the back button
 };

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

我试图使用以下方法找到要预订的日期列表:

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

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

当我使用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 );

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

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

下面是datepicker HTML的示例(将其全部添加都太长了!)

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

我真的被困在这里。任何建议都很好。

谢谢

2 个答案:

答案 0 :(得分:0)

请尝试:

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

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

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

答案 1 :(得分:0)

这是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.