问题描述
我正在尝试获取以下2个div元素内各项的文本内容,但收到一个找不到该元素的错误.例如,第一个div的文本内容是"Text1".我如何获得该文本?
I am trying to get the text content of the items inside the following 2 div elements but getting an error that the element cannot be found. For example the text content for first div is "Text1". How can I get that text?
到目前为止,我已经尝试过:
So far, I have tried:
driver.findElement(By.xpath("//*[@id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]")).getText())
并且抱怨找不到该元素.
and that complains of not finding that element.
这是html代码:
<div class="notesData">
<div class="notesDate" data-bind="text: $.format('{0} - {1}', moment(Date).format('MMM DD, YYYY h:mm a'), User)">Text1</div>
<div class="notesText" data-bind="text: Note">Text2 on next line</div>
</div>
这是错误,我得到:
推荐答案
不要使用无意义的XPath.您的错误消息告诉您"NoSuchElement",因此您的定位器错误,这与getText无关(尚未).尝试使用CssSelector或有意义的XPath:
Don't use meaningless XPath. Your error message tells you "NoSuchElement", so you had your locator wrong, which has nothing to do with getText (not yet). Try using CssSelector or meaningful XPath:
driver.findElement(By.cssSelector("#ctrlNotesWindow .notesData > .notesDate")).getText();
这篇关于如何使用Selenium Webdriver从以下div获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!