java - Selenium Java-空值的文本字段验证和日期验证
问题描述
我想验证:1)如果 from city 为空白,则用户收到警告并且程序退出 2)出发日期不为空或小于返回日期
这是代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class e2e {
public static WebDriver driver;
public static void main(String[] args) {
//Go to URL
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
driver.get("https://www.spicejet.com/");
//Travel city pickers
//Question1:If From city is blank generate warning
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXTaction")).click();
WebElement fromCity=driver.findElement(By.xpath("//a[@value='ATQ']"));
//driver.findElement(By.xpath("//a[@value='ATQ']")).click();
driver.findElement(By.xpath("//a[@value='']")).click();
String fromCt=fromCity.getText();
if(fromCt.equals(" ")){
System.out.println("You did not enter tarvelling from city");
driver.quit();
}
else{
driver.findElement(By.xpath("(//a[@value='DEL'])[2]")).click();}
//Travel date pickers
JavascriptExecutor jsLeave= (JavascriptExecutor)driver;
jsLeave.executeScript("document.getElementById('ctl00_mainContent_view_date1').value='12-05-2019'");
JavascriptExecutor jsReturn= (JavascriptExecutor)driver;
jsReturn.executeScript("document.getElementById('ctl00_mainContent_view_date2').value='18-05-2019'");
}
}
问题:当我尝试使用这条线时,应该从城市空我没有收到警告但没有这样的元素:无法定位元素故障。driver.findElement(By.xpath("//a[@value='']")).click();
第二个问题是目前 05/12/2019 正在填充但没有返回日期,该字段没有任何内容。
如果你有时间,我可以在这里使用一些帮助。提前致谢。
解决方案
这是您检查第一种情况的方法,而第二种情况永远不会发生,因为返回日期字段将禁用您出发日期之前的所有日期。
driver.get("https://spicejet.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// click on search
driver.findElement(By.id("ctl00_mainContent_btn_FindFlights")).click();
// check from city error message
if (driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).getText().equals("")) {
if (driver.findElement(By.id("view-origin-station")).getText().equals("Select Departure City")) {
System.out.println("Pass - Select Departure City message displayed as user did not enter from city.");
}else {
System.out.println("Fail - Select Departure City message not displayed when user not enter from city.");
}
}
// enter from city
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.xpath("//a[contains(.,'ATQ')]")).click();
// enter to city
driver.findElement(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT")).click();
driver.findElement(By.xpath("//a[contains(.,'GOI')]")).click();
推荐阅读
- php - 从 PHP 文件中的 $_POST 数据调用 $_SESSION 变量?
- javascript - 使用 GET 根据嵌套字段返回记录
- scala - 对于 Scala 2.13,更新具有数百万更新的 LongMap、HashMap 或 TrieMap 的最快方法是什么?
- python - PySide2:如何在 QWebEngineView 中嵌入 HTML5 视频
- python - 无法在 python 3.8 上安装熊猫
- web-scraping - 使用 Google 表格进行网页抓取的指南
- javascript - 如何使 vis.js 网络上的 Edges 不可选择?
- token - 什么是区块链中的插槽?
- java - 对编写存储字符的 ListADT 感到困惑
- phpmyadmin - 如何加密phpmyadmin中的表