找到元素后也无法单击-Python硒

时间:2020-04-28 01:38:57

标签: python selenium xpath click

一段时间以来,我一直为更多加载按钮感到痛苦。我希望创建一个循环,在其中单击Linkedin页面的技能部分上的“加载更多”。但是,只是未始终单击该按钮。

我的印象是问题在于该元素在页面上不可见。因此,我有一个分段滚动,该滚动继续向下移动页面,直到找到该元素。但是令人困惑的是,即使页面现在移到正确的位置,也没有单击该元素。没有引发任何错误。

我已经尝试了几乎每个版本的元素位置(xpath,类名,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)

1 个答案:

答案 0 :(得分:1)

@Datanovice提出的使用javascript强制单击的建议非常有用。最初,当我尝试调整解决方案时,收到错误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)