Selenium + C#:如何通过循环滚动元素中的值?

时间:2018-06-04 14:38:58

标签: c# selenium-webdriver

我使用for循环向下滚动它。在for循环中,我使用Actions SendKeys:

action.SendKeys(OpenQA.Selenium.Keys.Down).Build().Perform();

如何在循环中向下滚动的每个元素中获取值? 在滚动中,一次只显示15个元素。

enter image description here

非常感谢您阅读我的问题!

此卷轴的HTML:

    <div id="contactList" class="web z-data-list" tabindex="30" style="position: relative;">
    <div style="overflow: visible; height: 0px; width: 0px;">
        <div class="virtualized-scroll testing" style="height: 679px; width: 304px;">
            <div aria-label="grid" class="ReactVirtualized__Grid ReactVirtualized__List" role="grid" tabindex="-2" style="box-sizing: border-box; direction: ltr; height: 679px; position: absolute; width: 304px; will-change: auto; overflow: auto;">
                <div class="ReactVirtualized__Grid__innerScrollContainer" style="width: auto; height: 2198px; max-width: 304px; max-height: 2198px; overflow: hidden; position: relative;">
                    <div style="height: 44px; left: 0px; position: absolute; top: 0px; width: 100%;">
                        <div class="contact-list__add flx flx-al-c nav-button_add_friend clickable  ">
                            <i class="fa fa-icon-add-friend contact-list__add__icon "></i>
                            <span class="contact_add_friend_item" data-translate-inner="STR_CONTACT_ADD_FRIEND">Thêm bạn</span>
                        </div>
                    </div>
                    <div style="height: 36px; left: 0px; position: absolute; top: 44px; width: 100%;">
                        <div class="data-list__label" style="font-size: 15px; line-height: 43px;">
                            <span>
                                <span data-translate-inner="STR_SEND2ME_FROM">Gửi file giữa di động và máy tính</span>
                            </span>
                            <span style="cursor: pointer; float: right; font-size: 15px; left: -2px; color: rgb(140, 149, 163); position: relative; top: 3px;"></span>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 80px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/e/2/2/b/28/120/2743b59acfcc5bde9357c52d09a9c305.jpg&quot;);"></div>
                                <div class="avatar__oa-verified"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">
                                <span data-translate-inner="KEY_NAME_SEND2ME">File Tranfer</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 42px; left: 0px; position: absolute; top: 152px; width: 100%;">
                        <div class="data-list__label" style="font-size: 15px; line-height: 43px;">
                            <span>
                                <span data-translate-inner="STR_CONTACT_FRIENDS">Friend(50)</span> 
                            </span>
                            <span style="cursor: pointer; float: right; font-size: 15px; left: -2px; color: rgb(140, 149, 163); position: relative; top: 3px;">
                                <i class="fa fa-chevron-up"></i>
                            </span>
                        </div>
                    </div>
//begin element i want to get
                    <div style="height: 72px; left: 0px; position: absolute; top: 194px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/4/8/c/f/27/120/c5ee9f2b5a5a87d227fe2faf376f2e2c.jpg&quot;);"></div>
                            </div>
                            <div class="list-friend-conctact friend_onlines_dot"></div>
                            <div class="item-title list-friend-conctact__title has-status ">Bùi Phúc Thuận Châu
                                <span class="friend_online_status truncate" style="top: 0px; left: 3px;">Snvv nha 3. Yêu yêu. ❤❤ ;p;p;p</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
//end element ! 
                    <div style="height: 72px; left: 0px; position: absolute; top: 266px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/8/7/e/b/22/120/c36f271edfcc8e9843c81cd37997db69.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title has-status ">Cá
                                <span class="friend_online_status truncate" style="top: 0px; left: 3px;">:*:* mình hát như một con điên</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 338px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/d/7/e/5/109/120/e9c457d6b2ab4c9cc8752bc03152efca.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Chiến Ngáo</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 410px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/3/a/8/f/7/120/7b5693defcb6d8811523f303610ab4d4.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Cô Trang Lý</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 482px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/7/2/c/1/5/120/9abd88c1b2d7a6712dfbf2217ba85b28.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Cô Xuân "English" Ko Bik</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 554px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/4/4/7/e/8/120/9f207215cd42a435771e674ad3ba0f36.jpg&quot;);"></div>
                            </div>
                            <div class="list-friend-conctact friend_onlines_dot"></div>
                            <div class="item-title list-friend-conctact__title ">Dũng A12</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 626px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/9/b/f/8/37/120/aba8581271341aa448dd65c31bf8994e.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title has-status ">Dung Mama
                                <span class="friend_online_status truncate" style="top: 0px; left: 3px;">mưa.......nhớ.......</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 698px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/d/b/3/f/91/120/f0f272dcf100fd3fe756fdc6d241a5f9.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title has-status ">Dung Sd
                                <span class="friend_online_status truncate" style="top: 0px; left: 3px;">Nghe Dung hát nhé.. :* :*</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 770px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/1/c/a/8/24/120/8f205cd41856a866ad2b512f3b670702.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Đặng Bình</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 842px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/8/5/f/c/50/120/219d467feb2241eebc8705cfa467d48b.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title has-status ">Hải Yến
                                <span class="friend_online_status truncate" style="top: 0px; left: 3px;">text-status.</span>
                            </div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 914px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/4/d/9/1/29/120/66275bb8ca9fcca1bd9bdaee473ac464.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Hello Smile</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                    <div style="height: 72px; left: 0px; position: absolute; top: 986px; width: 100%;">
                        <div class="item minimized rel list-friend-conctact  ">
                            <div class="avatar avatar--m list-friend-conctact__avatar">
                                <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/f/a/7/7/15/120/ebfcb8922a0ec8bf9c52b96d35ec3e29.jpg&quot;);"></div>
                            </div>
                            <div class="item-title list-friend-conctact__title ">Hiền</div>
                            <div class="func-setting-container">
                                <div class="func-setting">
                                    <div class="item-action__menu ">
                                        <i class="fa fa-chat-menu-more" style="font-size: 19px;"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="item-action group">
                                <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="scroll-content" style="position: relative; overflow: hidden; width: 12px; height: 679px;">
                <div style="position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; overflow: scroll; margin-right: -7px; margin-bottom: -7px;">
                    <div class="scrollbar-container" style="height: 3794px;"></div>
                </div>
                <div style="position: absolute; height: 6px; transition: opacity 200ms; opacity: 0; right: 2px; bottom: 2px; left: 2px; border-radius: 3px;">
                    <div style="position: relative; display: block; height: 100%; cursor: pointer; border-radius: inherit; background-color: rgba(0, 0, 0, 0.2); width: 0px;"></div>
                </div>
                <div style="position: absolute; width: 6px; transition: opacity 200ms; opacity: 0; right: 2px; bottom: 2px; top: 2px; border-radius: 3px;">
                    <div style="position: relative; display: block; width: 8px; background-color: rgb(204, 204, 204); border-radius: 3px; height: 121px; transform: translateY(0px);"></div>
                </div>
            </div>
        </div>
    </div>
    <div data-tooltip="" data-position="" class="popover-container func-setting popover-container" id=""></div>
    <div class="resize-triggers">
        <div class="expand-trigger">
            <div style="width: 305px; height: 680px;"></div>
        </div>
        <div class="contract-trigger"></div>
    </div>
</div>

再次感谢!这个问题有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

您可以使用

IList<IWebElement> List = driver.FindElements(By.Id("contactList"));
List<String> NewList = new List<String>();
foreach (var item in List)
        {
            NewList.Add(item.Text);
        }
Actions action1 = new Actions(driver);
action1.MoveToElement(NewList[<Index number>]).Perform();
action1.Click(NewList[<Index number>]).Perform();