Selenium Java比较不为真?

huangapple go评论88阅读模式
英文:

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(&quot;https://www.hotel.de/&quot;);
boolean status = false;
String searchText = &quot;Hannover, Niedersachsen&quot;; 
WebElement inputBox = driver.findElement(By.xpath(&quot;//div[@class=&#39;LocationAutoSuggest__container--2Hli_&#39;]//input&quot;));

Now i send String "Hannover" to Searchbox:

Actions actions = new Actions(driver);
actions.moveToElement(inputBox).click().sendKeys(&quot;Hannover&quot;).build().perform();
Thread.sleep(3000);
List&lt;WebElement&gt; listsearch = driver.findElements(By.id(&quot;react-autowhatever-1&quot;));

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(&quot;hooho&quot;);
    	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

  1. 如果您的列表大小为零,则直接打印“false”。
  2. 如果大小不大于零,请检查您的定位器。
英文:
  1. Check the size of your List if it zero than directly false will be printed.
  2. If size is not greater than zero check your locator.

huangapple
  • 本文由 发表于 2020年7月29日 20:43:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63153891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定