如何使用Selenium和Python点击linkedin概要文件中的按钮



一个非常快速的问题。我想用selenium点击我的LinkedIN配置文件中的一个按钮来扩展一个区域。不幸的是,我总是出错。在下面的按钮,我正试图点击:

<button class="pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle link link-without-hover-state" aria-expanded="false" type="button">Mehr anzeigen
<li-icon aria-hidden="true" type="chevron-down-icon" class="pv-profile-section__toggle-detail-icon" size="small"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" data-supported-dps="16x16" fill="currentColor" width="16" height="16" focusable="false">
<path d="M8 9l5.93-4L15 6.54l-6.15 4.2a1.5 1.5 0 01-1.69 0L1 6.54 2.07 5z"></path>
</svg></li-icon></button>

到目前为止,这就是我一直在尝试的:

driver.find_element_by_class_name("pv-profile-section__actions-inline ember-view")

不幸的是,这并没有奏效。你有什么想法吗?

所需的元素是一个动态元素,因此要点击该元素,您必须诱导WebDriverWait等待element_to_be_clickable(),您可以使用以下定位器策略之一:

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pv-profile-section__see-more-inline.pv-profile-section__text-truncate-toggle.link.link-without-hover-state"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle link link-without-hover-state' and contains(., 'Mehr anzeigen')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

更规范地说,您可以使用:

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pv-profile-section__see-more-inline.pv-profile-section__text-truncate-toggle.link.link-without-hover-state li-icon.pv-profile-section__toggle-detail-icon"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle link link-without-hover-state' and contains(., 'Mehr anzeigen')]//li-icon[@class='pv-profile-section__toggle-detail-icon']"))).click()
    

最新更新