首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用selenium webdriver捕获span内的文本

使用selenium webdriver捕获span内的文本
EN

Stack Overflow用户
提问于 2016-08-18 19:51:31
回答 1查看 1.4K关注 0票数 1

我正在使用selenium webdriver和它的新手。我需要帮助。我的Xpath如下所示:

代码语言:javascript
复制
html/body/div[1]/section/div/div/div/div/div[1]/div[1]/div[2]/ul/li[1]/div/span

代码:

代码语言:javascript
复制
 <div class="mod-content">
   <ul id="issuedetails" class="property-list two-cols">
      <li class="item">
         <div class="wrap">
            <strong class="name">Type:</strong>
               <span id="type-val" class="value">
                  <img width="16" height="16" title="Provision access - HUE"         src="/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype" alt=""/>
                   Provision access - HUE           
               </span>
          </div>

我想要捕获文本"Provision access - HUE“。但是当我使用Selenium webdriver 2时

代码语言:javascript
复制
WebElement Type = driver.findElement(By.xpath("//*/span[@id='type-val]"));   
System.out.println("Type is "+Type.getText());//Printing Type

我得到的输出是"Type is Access Request“。

EN

回答 1

Stack Overflow用户

发布于 2016-08-18 19:58:24

我认为有多个span元素的类名为value,而您正在定位其他span,因为findElement总是返回第一个匹配的定位器元素,您需要创建唯一的定位器来定位您想要的元素,可能id在这里是唯一的,所以您应该尝试使用By.id(),如下所示:

代码语言:javascript
复制
WebElement Type = driver.findElement(By.id("type-val"));   
System.out.println("Type is "+Type.getText());

如果id在这里也不是唯一的,请尝试使用下面的xpath :-

代码语言:javascript
复制
WebElement Type = driver.findElement(By.xpath(".//strong[contains(text(), 'Type:')]/following-sibling::span[@id = 'type-val']"));   
System.out.println("Type is "+Type.getText());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39017498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档