首页 > 解决方案 > Selenium 中的导航错误:链接在点 (...) 处不可点击。其他元素会收到点击

问题描述

我想让 Selenium 浏览此页面导航栏的项目,然后检索其内容并将它们传递给 beatifulsoup。

我的代码在第一页上运行良好,之后出现以下错误:

ElementClickInterceptedException: Message: element click intercepted: Element <a href="https://www.rollingstone.com/music/music-lists/500-greatest-songs-of-all-time-151127/smokey-robinson-and-the-miracles-shop-around-71184" style="color: rgb(211, 37, 49); padding: 4px 8px;">...</a> is not clickable at point (77, 73). Other element would receive the click: <div class="fc-dialog-overlay"></div>

以前的答案没有帮助。这是我的代码。我真的很感激任何帮助。

webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

target_page = 'https://www.rollingstone.com/music/music-lists/500-greatest-songs-of-all-time-151127/'

driver.get(target_page)
header = driver.find_element_by_id('pmc-gallery-list-nav-bar-render')
items = header.find_elements_by_tag_name('a')

songs = []

for item in items:    
    driver.maximize_window()
    time.sleep(5)
    item.click()
    page_source = driver.page_source
    soup = BeautifulSoup(page_source)
    song_all = soup.find_all('h2', {'class':'c-gallery-vertical-album__title'})
    for song in song_all:
        songs.append(strip_extra_chars(song.get_text()))

driver.quit()

标签: seleniumweb-scrapingselenium-chromedriver

解决方案


让我们评论一下options.add_argument('--headless'),看看程序是如何运行的。您需要确定哪个元素与您的链接重叠。例如,我尝试运行您的代码,然后出现此对话框:

弹出窗口

在这种情况下,要禁用通知,您可以添加:

prefs = {"profile.default_content_setting_values.notifications": 2}
options.add_experimental_option("prefs", prefs)

但是此对话框与定位器不匹配.fc-dialog-overlay,因此您的问题可能与另一个元素有关。你需要找到它。

另一种解决方法,可以点击使用jsdriver.execute_script()


推荐阅读