英文:
Selenium Java compare not true?
问题
我尝试使用下面的代码比较一些文本:
driver.get("https://www.hotel.de/");
boolean status = false;
String searchText = "Hannover, Niedersachsen";
WebElement inputBox = driver.findElement(By.xpath("//div[@class='LocationAutoSuggest__container--2Hli_']//input"));
现在我将字符串"Hannover"发送到搜索框:
Actions actions = new Actions(driver);
actions.moveToElement(inputBox).click().sendKeys("Hannover").build().perform();
Thread.sleep(3000);
List<WebElement> listsearch = driver.findElements(By.id("react-autowhatever-1"));
然后将找到的文本与searchText进行比较:
for(WebElement listElem : listsearch) {
System.out.println(listElem.getText());
System.out.println(searchText.equals(listElem.getText()));
if(searchText.equals(listElem.getText())) {
System.out.println("hooho");
status = true;
break;
} else {
status = false;
}
}
System.out.println(status);
==> 你能告诉我,为什么我得到了FALSE而不是TRUE吗?(我如何查看日志,以了解实际进行了什么比较?)非常感谢。
英文:
i'm trying to compare some texts with this code bellow:
driver.get("https://www.hotel.de/");
boolean status = false;
String searchText = "Hannover, Niedersachsen";
WebElement inputBox = driver.findElement(By.xpath("//div[@class='LocationAutoSuggest__container--2Hli_']//input"));
Now i send String "Hannover" to Searchbox:
Actions actions = new Actions(driver);
actions.moveToElement(inputBox).click().sendKeys("Hannover").build().perform();
Thread.sleep(3000);
List<WebElement> listsearch = driver.findElements(By.id("react-autowhatever-1"));
And compare the found text with searchText :
for(WebElement listElem : listsearch) {
System.out.println(listElem.getText());
System.out.println(searchText.equals(listElem.getText()));
if(searchText.equals(listElem.getText())) {
System.out.println("hooho");
status = true;
break;
} else {
status = false;
}
}
System.out.println(status);
==> Could you tell me: why i become FALSE instead of TRUE? (How can i see the logs, to know what was actually compared?). Many thanks.
答案1
得分: 2
- 如果您的列表大小为零,则直接打印“false”。
- 如果大小不大于零,请检查您的定位器。
英文:
- Check the size of your List if it zero than directly false will be printed.
- If size is not greater than zero check your locator.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论