这是我为了使用 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();
尝试使用 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()
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);