即使找到它也无法点击元素 - Python selenium

Unable to click on element even after finding it- Python seleneium

加载更多按钮让我痛苦了一段时间。我希望创建一个循环,我在 Linkedin 页面的技能部分单击 "load more"。然而,这个按钮并不是一直被点击。

我的印象是问题是该元素在页面上不可见。所以,我有一个分段滚动,它继续向下移动页面直到找到元素。但令人困惑的是,即使页面现在移动到正确的位置,也没有点击该元素。没有错误被抛出。

我几乎尝试了元素位置的每个版本(xpath、class 名称、css 选择器、完整的 xpath)。如果按钮在页面上可见,为什么不会被点击?

相关代码:

##log into Linkedin

linkedin_urls=['https://www.linkedin.com/in/julie-migliacci-revent/']

ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('C:\Users\Root\Downloads\chromedriver.exe')

driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
driver.maximize_window()

WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "session_key"))).send_keys("EMAIL")
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "session_password"))).send_keys("PASSWORD")
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[@class='btn__primary--large from__button--floating']"))).click()


linkedin_urls=['https://www.linkedin.com/in/julie-migliacci-revent/', 'https://www.linkedin.com/in/kathleen-meyers-126a7931']


for linkedin_url in linkedin_urls:
   driver.get(linkedin_url)

   looking_for_element = True
   ldmore = ''

   while looking_for_element:
       elements = driver.find_elements_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]')

       if len(elements) > 0:
          ldmore = elements[0]
          ldmore.click()
          looking_for_element = False
       else:
           global_copyright = driver.find_elements_by_css_selector('#globalfooter-copyright')

           if len(global_copyright) > 0:
               looking_for_element = False
           else:
               body = driver.find_element_by_css_selector('body')
               sleep(5)
               body.send_keys(Keys.PAGE_DOWN)

当底层解决方案不可见时,我还没有看到关于元素问题的讨论。代码设计为在找到元素后停止——并且正在正确执行此操作。但它只是没有点击元素。我不确定这是为什么。

我尝试过的地点:

absolute xpath:
driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]').click()

relative xpath: 
//span[contains(text(),'Show more')]

class name: 
pv-profile-section__card-action-bar pv-skills-section__additional-skills artdeco-container-card-action-bar artdeco-button artdeco-button--tertiary artdeco-button--3 artdeco-button--fluid" aria-controls="skill-categories-expanded

css: 
body.render-mode-BIGPIPE.nav-v2.theme.theme--classic.ember-application.boot-complete.icons-loaded:nth-child(2) div.application-outlet:nth-child(77) div.authentication-outlet:nth-child(3) div.extended div.body div.pv-profile-wrapper.pv-profile-wrapper--below-nav div.self-focused.ember-view div.pv-content.profile-view-grid.neptune-grid.two-column.ghost-animate-in main.core-rail div.profile-detail div.pv-deferred-area.ember-view:nth-child(6) div.pv-deferred-area__content section.pv-profile-section.pv-skill-categories-section.artdeco-container-card.ember-view div.ember-view > button.pv-profile-section__card-action-bar.pv-skills-section__additional-skills.artdeco-container-card-action-bar.artdeco-button.artdeco-button--tertiary.artdeco-button--3.artdeco-button--fluid

更新: 尝试了 JS 力,它点击了!但是抛出了错误:selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'click' of null

if len(elements) > 0:
    ldmore = elements[0]
    ldmorebtn = driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]').click()
    #driver.execute_script("arguments[0].checked = true;", ldmore)
    driver.execute_script("arguments[0].click();", ldmore)

@Datanovice 关于使用 java 脚本强制点击的建议非常有效。最初,当我尝试调整解决方案时,收到错误 selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'click' of null

这个错误是因为我一直在使用 EC.element_to_be_clickable。相反,当我将 java 方法与 EC.visibility_of_element_located 配对时,点击始终有效。

代码:

ldmore = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH,'xpath')))
driver.execute_script("arguments[0].click();", ldmore)