如何使用 NodeJS 在 Selenium 中自动化日期选择器?

问题描述 投票:0回答:2

我尝试自动化以下日期选择器: enter image description here

这是我为了使用 Selenium NodeJS 实现自动化而编写的代码:

const { By, Key, Builder, WebElement, Alert } = require('selenium-webdriver');
require('chromedriver');

async function login(){
  const driver = await new Builder().forBrowser('chrome').build();

  
  await driver.get('https://www.carnextdoor.com.au/search');

  await driver.findElement(By.name('location')).clear();

  let city = 'Sydney Airport, Airport Dr, Sydney, New South Wales 2020, Australia';
  await driver.findElement(By.name('location')).sendKeys(city);
  await driver.findElement(By.name('location')).sendKeys(Key.RETURN);


  

  await driver.findElement(By.css('.cyp-datetime-picker-modal')).click();

  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();
  await driver.findElement(By.css('.mbsc-cal-next-m')).click();

  
  


   await driver.quit();
}

login();
我可以切换几个月,但无法选择日期范围。如果有人能教我如何从日期选择器中选择日期范围,我将非常感激。 :)

javascript node.js selenium selenium-webdriver
2个回答
2
投票

尝试使用 xpath 来定位日期。能够选择日期。

driver.get("https://www.carnextdoor.com.au/search")
fromdate= "'Monday, August 2 '"
todate = "'Thursday, August 5 '"
driver.find_element_by_xpath("//button[@class='cnd-btn cnd-filter-btn cyp- datetime-picker-modal']").click()
driver.find_element_by_xpath("//div[@aria-label= {} and @aria- disabled='true']".format(fromdate)).click()
driver.find_element_by_xpath("//div[@aria-label= {} and @aria-disabled='true']".format(todate)).click()
driver.find_element_by_xpath("//button[text()='Confirm time']").click()

0
投票
let datePickerEndDateField = await driver.switchTo().activeElement();

console.log("\n\nthis Element className: " + await datePickerEndDateField.getAttribute("className"));
console.log("\n\nthis Element type: " + await datePickerEndDateField.getAttribute("type"));

//"06/12/2024 02:33 AM";

await datePickerEndDateField.sendKeys("06");
await datePickerEndDateField.sendKeys("/");
await datePickerEndDateField.sendKeys("12");
await datePickerEndDateField.sendKeys("/");
await datePickerEndDateField.sendKeys("2024");
await datePickerEndDateField.sendKeys(Key.ARROW_RIGHT);
await datePickerEndDateField.sendKeys("02");
await datePickerEndDateField.sendKeys(":");
await datePickerEndDateField.sendKeys("46");
await datePickerEndDateField.sendKeys("AM");
await datePickerEndDateField.sendKeys(Key.TAB);
© www.soinside.com 2019 - 2024. All rights reserved.