[英]How to get text from nested span tag in selenium
I have below HTML code in my project.我的项目中有以下 HTML 代码。
<button id="tab-oneWay" class="" data-initial-tab="true" type="button" data-section-id="#section-oneWay"> <span class="tab-label"> One Way <span class="visuallyhidden"> Tab 1 of 2</span> </span> </button>
I have to find only the text 'One Way'.我只需要找到文本“一种方式”。 I have tried below codes but it is returning 'One Way Tab 1 of 2'.我尝试了以下代码,但它返回“单向选项卡 1 of 2”。
findElement(By.id("tab-oneWay")).getText() findElement(By.cssSelector(".tab-label")).getText()
I am new in selenium so not sure how to find this.我是硒的新手,所以不知道如何找到它。 Any help on this would be great !对此有任何帮助都会很棒! Thanks in advance提前致谢
findElement(By.xPath("/button/span[contains(., 'One Way')]").getText()
您能否尝试使用 XPath 查找元素,如上所述,以查看是否正确返回?
Try this:试试这个:
spans = find_elements_by_tag_name("span")
span1_text = spans[0].get_text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.