我有以下HTML代码。
<span class="ng-binding" ng-bind="::result.display">All Sector ETFs</span>
<span class="ng-binding" ng-bind="::result.display">China Macro Assets</span>
<span class="ng-binding" ng-bind="::result.display">Consumer Discretionary (XLY)</span>
<span class="ng-binding" ng-bind="::result.display">Consumer Staples (XLP)</span>
可以看出,除了标签之间的文本之外,每个行的标签都是相同的。 如何根据标签之间的文本分别访问上述每一行。
答案 0 :(得分:2)
使用以下作为xpath
//span[text()='All Sector ETFs']
答案 1 :(得分:1)
您可以使用x-path函数text()。 例如
//span[text()="All Sector ETFs"]
找到第一个跨度
答案 2 :(得分:1)
您可以使用以下xPath根据文本
查找所需的元素String text = 'Your text';
//text may be ==>All Sector ETFs, China Macro Assets, Consumer Discretionary (XLY), Consumer Staples (XLP)
String xPath = "//*[contains(text(),'"+text+"')]";
通过这个你可以找到每个元素..
希望它会对你有所帮助.. :)
答案 3 :(得分:0)
嗨,请按以下方式进行操作
Way One
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
List<WebElement> mySpanTags = driver.findElements(By.xpath("ur xpath"));
System.out.println("Count the number of total tags : " + mySpanTags.size());
// print the value of the tags one by one
// or do whatever you want to do with a specific tag
for(int i=0;i<mySpanTags.size();i++){
System.out.println("Value in the tag is : " + mySpanTags.get(i).getText());
// either perform next operation inside this for loop
if(mySpanTags.get(i).getText().equals("Consumer Staples (XLP)")){
// perform your operation here
mySpanTags.get(i).click(); // clicks on the span tag
}
}
// or perform next operations on span tag here outside the for loop
// in this case use index for a specific tag (e.g below)
mySpanTags.get(3).click(); // clicks on the 4 th span tag
}
第二种方式
直接找到代码//span[text()='Consumer Staples (XLP)']