如何使用Selenium和Python单击linkedin配置文件中的按钮

如何使用Selenium和Python单击linkedin配置文件中的按钮,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,一个很快的问题。我想使用selenium在我的LinkedIN配置文件中单击一个按钮以扩展一个区域。不幸的是,我总是出错。在下面的对话框中,单击我尝试单击的按钮: <button class="pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle link link-without-hover-state" aria-expanded="false"

一个很快的问题。我想使用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>

不幸的是,这不起作用。你有什么想法吗?

所需的元素是一个动态元素,因此要单击该元素,你必须使
元素成为可点击的()
,并且你可以使用以下任一方法:

  • 使用
    CSS\u选择器

    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()
    
    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')]"))).click()
    
    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()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
更规范地说,您可以使用:

  • 使用
    CSS\u选择器

    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()
    
    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')]"))).click()
    
    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()
    

您会遇到哪些错误?