如何使用列表硒中包含的文本值打印元素c#

时间:2018-08-13 06:31:59

标签: c# selenium selenium-webdriver

有人知道如何打印出硒c#中具有文本值的列表中包含的所有元素吗?尝试执行下面的代码,将其打印出空白值。但是,如果我将writeline置于elem,则仅显示该值,但不是文本形式。我想从文本中获得价值。

代码:

IList<IWebElement> attachmentList = driver.FindElements(By.ClassName("comment-box"));

foreach (IWebElement element in attachmentList)
{
    Console.WriteLine(element.Text);
}

HTML:

    <div class="comment-box">
        <!-- Comment Image -->
        <div class="col-xs-2">
            <div id="attachmentImgSFHD-24" class="attachmentImg">
                <img src="downloadAttachment?attachmenturl=/secure/thumbnail/10111/_thumb_10111.png" />
            </div>
        </div>
        <!-- Attachment details -->
        <div  class="col-xs-10">
            <div class="commentContent">
                <div class="topRow">
                    <div class="username">ApplicationLink.png</div>
                    <div class="commentTimeStamp">31400 KB</div>
                </div>
                <div class="bottomRow">
                    <div class="commentDisplay">
                        <a href="downloadAttachment?filename=ApplicationLink.png&id=10111&mimeType=image/png&showAttachment=true" target="_blank">Download</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="comment-box">
        <!-- Comment Image -->
        <div class="col-xs-2">
            <div id="attachmentImgSFHD-24" class="attachmentImg">
                <img src="downloadAttachment?attachmenturl=/secure/thumbnail/10313/_thumb_10313.png" />
            </div>
        </div>
        <!-- Attachment details -->
        <div  class="col-xs-10">
            <div class="commentContent">
                <div class="topRow">
                    <div class="username">test.jpg</div>
                    <div class="commentTimeStamp">7423 KB</div>
                </div>
                <div class="bottomRow">
                    <div class="commentDisplay">
                        <a href="downloadAttachment?filename=test.jpg&id=10313&mimeType=image/jpeg&showAttachment=true" target="_blank">Download</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<br/>

3 个答案:

答案 0 :(得分:0)

元素在html中没有文本,因此element.Text为空。使用

Console.WriteLine(element.GetAttribute("value"));

答案 1 :(得分:0)

您可以使用下面的Xpath获取附件详细信息

Xpath: //div[@class='comment-box']//div[@class='commentContent']//div[@class='username']

代码:

IList<IWebElement> attachmentList = driver.FindElements(By.XPath("//div[@class='comment-box']//div[@class='commentContent']//div[@class='username']"));

foreach (IWebElement element in attachmentList)
{
    Console.WriteLine(element.Text);//It will print all the attachment name like 'ApplicationLink.png,test.jpg'
}

答案 2 :(得分:0)

IList<IWebElement> attachmentList = driver.FindElements(By.ClassName("comment-box"));

foreach (IWebElement element in attachmentList)
{
    System.Threading.Thread.Sleep(2000);
    Console.WriteLine(element.Text);
}

通过放置thread.sleep代码可以正常工作