2

我使用 Python 和 Selenium。任务是单击带有文本'+like' 的按钮或带有class ='profile-image' 的'td ' 。但是按钮没有 id 并且它的“更多喜欢”用于其他按钮。具有'profile-image-button'的div的情况相同(在其他'divs'中使用的div的类ID)。我试图获取'td' 的id :

button = photos.find('td', class_='profile-image')
print(button.get_id)

输出为“无”

这是网页的html代码:

<div id="category7515692" class="category-content" data-content="present" data-collapsing="true">
  <table class="pictures" data-columns-count="12" data-type="gallery">
    <tbody class="" data-selec="7565904" data-name="beauty" data-live="true">
      <tr data-mutable-id="MR1main" class="header">
        <td class="main-row-buttons" rowspan="1" data-mutable-id="Bmain">
          <table>
            <tbody>
              <tr>
                <td class="profile-image" id="view-75634" data-event-more-view="event-more-view" data-selec="7565904" islive="true" isseparatedbutton="false">
                  <div class="profile-image-button">
                    <span class="more-likes">+like</span>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
</div>

如何单击按钮或如何获取 id?

4

2 回答 2

0

假设只有一个带有“+like”文本的按钮,您可以搜索具有特定文本的元素,如下所示:

driver.find_element_by_xpath("//*[contains(text(), '+like')]").click()
于 2018-12-04T06:37:33.267 回答
0

所需的元素是一个React元素,因此要单击该元素,您必须诱导WebDriverWait以使该元素可单击,并且您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.profile-image>div.profile-image-button>span.more-likes"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='profile-image']//span[@class='more-likes' and contains(.,'+like')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
于 2018-12-04T09:42:51.770 回答