如何通过Selenium和Java提供的HTML单击img标签?

时间:2018-09-06 09:08:01

标签: java selenium xpath css-selectors webdriver

如何根据HTML单击<img>

<a aria-expanded="false" class="dropdown-toggle" data-toggle="dropdown" href="javascript: void(0);"> <span class="cat__top-bar__avatar" href="javascript:void(0);"> <img src="assets/modules/dummy-assets/common/img/avatars/1.jpg"> </span> </a>

完整的HTML:

<div class="cat__top-bar__right">                        
                    <!----><div class="cat__top-bar__item hidden-sm-down">
                        <div class="dropdown notification-dropdown">
                            <a aria-expanded="false" class="dropdown-toggle" href="javascript: void(0);" title="unread notifications">
                                <i class="menu-notification-icon icmn-Notification menu-left__icon"></i><!----><span class="badge badge-noti badge-danger"></span>
                            </a>
                            <div class="arrow-up notifications" id="drop-down-arrow"></div>
                            <ul aria-labelledby="" class="dropdown-menu dropdown-menu-right" id="drop-down" role="menu">
                                <div class="notification_bar">
                                    <ul>
                                        <li>Notificaitons (1112)</li>
                                        <li><a href="javascript: void(0);">Marks All as Read</a></li>
                                    </ul>
                                </div>
                                <!---->
                                <!---->
                                <div class="cat__top-bar__activity">
                                 
                                </div>
                                <a class="view-all-notify" href="#/portals/super-admin/all-notifications">View All Notificaitons</a>
                            </ul>
                        </div>
                    </div>

                    <div class="cat__top-bar__item">
                        <div class="dropdown cat__top-bar__avatar-dropdown">
                            <a aria-expanded="false" class="dropdown-toggle" data-toggle="dropdown" href="javascript: void(0);">
                                <span class="cat__top-bar__avatar" href="javascript:void(0);">
                                    <img src="assets/modules/dummy-assets/common/img/avatars/1.jpg">
                                </span>
                            </a>
                            <div class="arrow-up profile"></div>
                            
                            <!----><ul class="dropdown-menu dropdown-menu-right" id="profile-drop-down" role="menu">
                                <div class="dropdown-header"><a class="dropdown-item" href="#/portals/super-admin/profile"><i class="dropdown-icon icmn-Profile"></i> Profile</a></div>
                                <div class="dropdown-header"><a class="dropdown-item" href="#/admin"><i class="dropdown-icon icmn-Logout-icon"></i> Logout</a></div>                                
                            </ul>
                       
                        </div>
                    </div>
                </div>

2 个答案:

答案 0 :(得分:0)

根据您共享的 HTML ,您不应在click()标记上调用<img>,因为它没有onclick事件。相反,您应该在click()标记上调用<span>。您可以使用以下任一解决方案来执行click()

  • cssSelector

    driver.findElement(By.cssSelector("a.dropdown-toggle > span.cat__top-bar__avatar")).click();
    
  • xpath

    driver.findElement(By.xpath("//a[@class='dropdown-toggle']/span[@class='cat__top-bar__avatar']")).click();
    

答案 1 :(得分:0)

使用此xpath

driver.findElement(By.xpath("//img[@src='assets/modules/dummy-assets/common/img/avatars/1.jpg']")).click();

此外,您可以将其与明确等待

一起使用
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//img[@src='assets/modules/dummy-assets/common/img/avatars/1.jpg']")))
element.click()