java - 无法通过 selenium 和 Java 从 Make My Trip 网站中的返回日期日历中找到任何日期
问题描述
在这里,我尝试选择 4 月 12 日的返回日期。我尝试使用不同的自定义 xpath 和 css 但无法找到元素:
import java.util.List;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Make_my_trip {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "F:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.makemytrip.com/");
// Selecting Return date
driver.findElement(By.id("hp-widget__return")).click();
//WebDriverWait ws = new WebDriverWait(driver, 10);
//ws.until(ExpectedConditions.presenceOfElementLocated(By.id("dp1551508380301")));
Thread.sleep(2000);;
while (!driver.findElement(By.xpath("//span[@class ='ui-datepicker-month']")).getText().contains("April")) {
System.out.println("Return date selected ");
driver.findElement(By.xpath(
"div[@id='dp1551508898872']//span[@class='ui-icon ui-icon-circle-triangle-e'][contains(text(),'Next')"))
.click();
}
List<WebElement> returndate = driver.findElements(By.xpath("//a[@class ='ui-state-default']"));
int count = returndate.size();
for (int j = 0; j < count; j++) {
String returndatetext = driver.findElements(By.xpath("//a[@class ='ui-state-default']")).get(i).getText();
if (returndatetext.equalsIgnoreCase("12")) {
driver.findElements(By.xpath("//a[@class ='ui-state-default']")).get(i).click();
break;
}
}
}
PS:
- 如果我们使用显式等待获取“ org.openqa.selenium.InvalidSelectorException ”错误,那么现在使用
Thread.sleep(1000)
。 - 如果我们使用Xpath
//div[@class='ui-datepicker-group ui-datepicker-group-last']/div/a/span[contains(text(),'Next' )][1]
获取 org.openqa.selenium.ElementNotVisibleException: element not visible
解决方案
“元素不可见”。我也有这个错误。这意味着您的可搜索元素在当前快照中不可见。您应该专注于该元素或向下滚动直到元素出现在快照中
推荐阅读
- arrays - 从数组中返回匹配的文档
- dbus - NetworkManager:RsnFlags 和 WpaFlags 之间的区别
- python - 如何验证字符串并将其转换为特定格式python
- r - 根据条件创建新列
- python - 对产品和分组求和
- android - FlatList scrollToEnd 在带有 Redux 的 Android 中不起作用
- android - 与 API25 不同的项目中的低速
- javascript - 带有日期时间到月和日的PHP面向对象类
- javascript - 防止提交错误返回 Javascript
- visual-studio-code - VS Code、Prettier 和 Flow 不能很好地协同工作——我错过了什么?