我已经被一个加载更多按钮痛苦了一段时间。我希望创建一个循环,在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)
我没有在SO上看到过关于元素问题的讨论,当底层解决方案不是可见性。代码的设计是一旦元素被定位就会停止--而且是正确的执行这个功能。但它就是没有点击元素。我不知道这是为什么。
我试过的位置。
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
UPDATE:尝试了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提出的使用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)