我是selenium webdriver的自动化新手。我正在尝试在旅游网站上预订航班。 (ryanair.com)
我遇到了一个弹出式日期选择器。我可以使用.sendkeys进入当天,在点击日期输入上的任何字段后,它会触发弹出日历显示,如果输入的格式为dd / mm / yyyy并且我想输入“20042019”它只进入dd中的20,然后自动选择当前月份和年份作为自动完成并打开日历弹出窗口。
我读了几篇文章说这些日历通常有两种类型1. iframe 2. datepicker - 我认为ryanair上的日历是基于下面xpath的datepickers
//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"><!---->
我真的被困在这里了。任何建议都会很棒。
谢谢
这是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.
请尝试:
li[data-id='12-04-2019'] span
注意:添加到span标记。刚尝试通过控制台和工作。
document.querySelector("li[data-id='12-04-2019'] span").click()