简体   繁体   中英

Selenium C# class name appears multiple times

i use selenuim and i try without success to find the element class="user-card_photo" each time for click in new browser , but i can not do this. Can anyone help me to find with selenuim one way driver find the class class="user-card_photo"

  <body>
          <div id="search">
                <div class="justify search">
                    <div class="search_col">
                        <div class="user-card">
                            <div class="user-card_photo">

                            </div>
                            <div class="user-card_photo_section">

                            </div>
                        </div>                   
                        <div class="user-card">
                            <div class="user-card_photo">

                            </div>
                            <div class="user-card_photo_section">

                            </div>
                        </div>
                    </div>
                    <div class="search_col">
                        <div class="user-card">
                            <div class="user-card_photo">

                            </div>
                            <div class="user-card_photo_section">

                            </div>
                        </div>
                        <div class="user-card">
                            <div class="user-card_photo">

                            </div>
                            <div class="user-card_photo_section">

                            </div>
                        </div>
                        <div class="user-card">
                            <div class="user-card_photo">

                            </div>
                            <div class="user-card_photo_section">

                            </div>
                        </div>                 
                    </div>
                </div>
            </div>
        </body>

i would appreciate if someone put c# code to answer my question

You have several div-elements with the class "user-card_photo". It's not completely clear what you want from your question, but I guess you might need the following:

To get all divs with the class "user-card_photo" do this:

List<WebElement> photos = driver.findElements(By.className("user-card_photo"));

Now you can loop through all div-elements that have the class "user-card_photo":

for(WebElement photo : photos) {
    // do whatever you like with the single elements now
}

Or if you're interested in only one of the div-elements you can get them by index

WebElement photo01 = photos.get(0);
WebElement photo02 = photos.get(1);
...

It's Java Code, but it's pretty simple and a good practice for you to bring this into C# code ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM