2016-06-08 19 views
0

我有以下HTML代码。如何使用HTML标签之间的文本访问元素 - Selenium WebDriver

<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

u能张贴父HTML源代码中,这个跨度标签撒谎 –

回答

2

使用下面的XPath的

//span[text()='All Sector ETFs'] 
1

您可以使用XPath功能文本()来。 例如

//span[text()="All Sector ETFs"] 

找到第一跨度

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+"')]"; 

通过这个,你可以找到每个元素找到所需的元素..

希望它会帮助你.. :)

+0

其实文可能是一切。你正在使用包含,只要它匹配一个元素文本的一部分,它会发现一些东西。 – RemcoW

0

嗨,请做它像贝洛w^

一号

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)']

相关问题