考虑以下代码段
<div class="contentInput medium wrapText">
<label class="ng-binding">Number1</label>
<div title="10000" class="readOnlyInput clearB wrapText text-left ng-binding">10000</div>
</div>
<div class="contentInput medium wrapText">
<label class="ng-binding">Number2</label>
<div title="10050" class="readOnlyInput clearB wrapText text-left ng-binding">10050</div>
</div>
<div class="contentInput medium wrapText">
<label class="ng-binding">Number2</label>
<div class="readOnlyInput clearB wrapText text-left ng-binding">....</div>
</div>
以下是我的问题:
答案 0 :(得分:1)
请尝试以下代码:
public string getLabelValue(String labelText)
{
List <WebElement> outerDiv= driver.findElements(By.xpath("//div[@class='contentInput medium wrapText']"));
for (WebElement div: outerDiv) {
if(div.findElements(By.tagName("label").getText().equals(labelText))){
return div.findElements(By.tagName("div").getText();
}
}
return "Label Not Found".
}
答案 1 :(得分:1)
它应该非常简单如下:
String labelText = driver.findElement(By.xpath("//div[label = 'Number1']/div")).getText();
您甚至可能不需要方法,因为它只是一行。如果您想要其他标签的div
值,例如Number2
,只需替换该值。
使用xpath
的方式使其更简单。
答案 2 :(得分:0)
嗨,请尝试如下
// taking under consideration that we have same number of label
// tag with its corresponding div tag
// Take label tag and div tag in list
List<WebElement> myLabels = driver.findElements(By.cssSelector(".ng-binding"));
List<WebElement> myDivs = driver.findElements(By.cssSelector(".readOnlyInput.clearB.wrapText.text-left.ng-binding"));
String UserInputLabelValue = "Number2";
// now size of myLabels = myDivs hence
for(int i=0;i<myLabels.size();i++){
if(myLabels.get(i).getText().equals(UserInputLabelValue)){
System.out.println("Div value is against " + UserInputLabelValue + " is : " + myDivs.get(i).getText());
}
}