gpt4 book ai didi

java - Selenium 测试 - 无法从日历列表中选择值

转载 作者:行者123 更新时间:2023-11-28 21:24:51 25 4
gpt4 key购买 nike

我无法从日历列表中选择日期值。所有其他测试均有效。

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class ch1 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C://testing/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
driver.get("https://www.via.com");
try {
driver.findElement(By.xpath("//div[@class='wzrk-button-container']/button[1]")).click();
}
finally {
driver.findElement(By.xpath("//div[@class='element relElements airportElements']/input[1]")).sendKeys("BLR");
driver.findElement(By.xpath("//div[@class='calendar-icon']")).click();
Select s = new Select(driver.findElement(By.xpath("//*[@id='depart-cal']/div[3]")));
s.selectByValue("19");

driver.wait(5000);
}
}
}

文本正在输入,日历正在打开,但没有选择日期。

最佳答案

这里的 calendar 不是一个 select 元素,它是一个 div 元素。所以 select 在这种情况下不起作用。

试试下面的代码:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ch1 {

static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
try{
System.setProperty("webdriver.chrome.driver", "C://testing/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,15);
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
driver.get("https://www.via.com");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='wzrk-button-container']/button[1]")));
driver.findElement(By.xpath("//div[@class='wzrk-button-container']/button[1]")).click();
driver.findElement(By.xpath("//div[@class='element relElements airportElements']/input[1]")).sendKeys("BLR");
driver.findElement(By.xpath("//div[@class='calendar-icon']")).click();
WebElement date = driver.findElement(By.xpath("//*[@id='depart-cal']/div[3]//div[text()='19']"));
wait.until(ExpectedConditions.visibilityOf(date));
date.click();

}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
driver.quit();
}
}

}

让我知道它是否适合您。

关于java - Selenium 测试 - 无法从日历列表中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43434780/

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