Webelement List并只选择一个

问题描述 投票:0回答:1

有人可以帮我处理以下情况:我有Webelement列表,其中包含以下内容:

Double Chance (Ordinary Time)
1X2 (Ordinary Time)
12 (Full Event)
Odd/Even (Full Event)
Over / Under (Full Time)
Odd/Even (Ordinary Time)
Halftime / Fulltime (Ordinary Time)
Over Under (Ordinary Time)
1X2 (1st Half)
Odd/Even (1st Half)

我必须确认它们之间存在,即1X2(普通时间),如果存在,那么只是为了使用它。如果不存在,请完成测试。下面是我试过的代码,但没有成功:

List<WebElement> allAvailsbleMarkets =  driver.findElements(By.xpath("//main[@class='be-root__main']/be-match2/be-single-view-layout/div[3]
 for (int p = 0; p < allAvailsbleMarkets.size(); p++) {
 System.out.println(allAvailsbleMarkets.get(p).getText());
 }
  if (allAvailsbleMarkets.contains("1X2 (Ordinary Time)")) {
                "GO to that element"
 } else {
 System.out.println("chosen Bet Type is not in the list of offered ones");
 }

不幸的是,我总是找不到那个元素。我做错了什么?

@ f1sh它正在检查所有选项,但我只需要一个:

Tip can not be posted
Tip can not be posted since that chosen Bet Type is not in the list of offered ones
Tip can not be posted since that chosen Bet Type is not in the list of offered ones
Tip can not be posted since that chosen Bet Type is not in the list of offered ones
Tip can not be posted since that chosen Bet Type is not in the list of offered ones
And until it finish counting
java selenium-webdriver
1个回答
2
投票

你检查一下ListWebElement包含String。这将永远是错误的,因为它包含WebElement类型的项目。

检查每个WebElement,如果它的getText()方法返回一个值equals你想要的String

for(WebElement e:allAvailsbleMarkets){
  if("1X2 (Ordinary Time)".equals(e.getText())){
     //go to that element

     //an element has been found, so stop the checking loop:
     break;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.