gpt4 book ai didi

javascript - 如何通过 Selenium webdriver 处理 Abhibus 回程的第二个日期选择器?

转载 作者:行者123 更新时间:2023-12-02 00:57:57 25 4
gpt4 key购买 nike

我正在编写代码来选择旅程日期和回程日期,但回程没有被选择。司机直接跳转到搜索按钮,无需输入返回日期。

public class Callender {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.abhibus.com/");
//to select leaving from
WebElement source =driver.findElement(By.xpath("//*[@id='source']"));
source.clear();
source.sendKeys("Tenal");
Thread.sleep(2000);
source.sendKeys(Keys.ENTER);
//to select destination
WebElement destination =driver.findElement(By.xpath("//*[@id='destination']"));
destination.clear();
destination.sendKeys("Hyderaba");
Thread.sleep(2000);
destination.sendKeys(Keys.ENTER);


WebElement element = driver.findElement(By.xpath("//*[@id=\"datepicker1\"]"));
String journeydate="04-10-2019";
selectJourney(driver,element,journeydate);

Thread.sleep(3000);
WebElement element1 = driver.findElement(By.xpath("//*[@id=\"datepicker2\"]"));
String returndate="06-10-2019";
selectRJourney(driver,element1,returndate);
//to click search button
driver.findElement(By.xpath("//*[@id=\"roundTrip\"]/a")).click();
}
public static void selectRJourney(WebDriver driver, WebElement element1, String returndate) {
JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript("arguments[0].setAttribute('value','"+returndate+"');", element1);
// TODO Auto-generated method stub

}
public static void selectJourney(WebDriver driver,WebElement element,String journeydate) {
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('value','"+journeydate+"');",element);
}

你能帮我找出哪里出了问题或者我可能做错了什么吗?

最佳答案

请使用以下代码。通过使用 document.getElementById('datepicker1').value = "Your_Date" 设置日历值。

String journeydate="04-10-2019";
selectJourney(driver,journeydate);

Thread.sleep(3000);
String returndate="06-10-2019";
selectRJourney(driver,returndate);

我修改了您的代码:

public static void selectRJourney(WebDriver driver, String returndate) {
JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript("document.getElementById('datepicker2').value = '"+returndate+"';");

}
public static void selectJourney(WebDriver driver,String journeydate) {
JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript("document.getElementById('datepicker1').value = '"+journeydate+"';");

}

建议:

  • 您可以轻松地使用 id,而不是使用 xpath

  • JavascriptExecutor如果想真正执行的话不推荐
    测试。

  • selectRJourneyselectJourney 是重复的方法并包含相同的代码。尝试将它们合并为一个。

************************ 已编辑 ********** **************

String journeydate="04-10-2019";
selectDateFromCalender(driver,"datepicker1",journeydate);

Thread.sleep(3000);
String returndate="06-10-2019";
selectDateFromCalender(driver,"datepicker2",returndate);

您可以创建通用方法 selectDateFromCalender 并将日期选择器 ID 作为参数传递:

public static void selectDateFromCalender(WebDriver driver, String datePickerId, String returndate) {
JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript("document.getElementById('"+datePickerId+"').value = '"+returndate+"';");

}

***** 根据您的线索要求进行黑客攻击 ******

driver.findElement(By.id("datepicker1")).click();
int currentSelectedDate = Integer.parseInt(driver.findElement(By.cssSelector(".ui-state-highlight")).getText());
driver.findElement(By.cssSelector(".ui-state-highlight")).click();
driver.findElement(By.id("datepicker2")).click();
if(currentSelectedDate>=30)
driver.findElement(By.xpath("//a[@class='ui-state-default'][contains(text(),'1')]")).click();
else
driver.findElement(By.xpath("//a[@class='ui-state-default'][contains(text(),'"+(currentSelectedDate+1)+"')]")).click();

关于javascript - 如何通过 Selenium webdriver 处理 Abhibus 回程的第二个日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57795659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com