如何定位并点击Gettyimages下一页按钮?解决Selenium点击拦截异常

页面链接说明
- 测试链接:https://www.gettyimages.dk/search/2/image?phrase=lfc
- 另一种链接格式:https://www.gettyimages.dk/photos/lfc?assettype=image&phrase=lfc&sort=mostpopular&license=rf,rm&page=1
目前仅测试第一个链接,不确定是否因链接格式导致点击失败。
尝试的定位方式及错误信息
CSS选择器1
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > div.content_wrapper > section > div > main > div > div > div:nth-child(4) > div.Gallery-module__columnContainer___LqU0P > section > a'))).click()
Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<a class="PaginationRow-module__button___QQbMu PaginationRow-module__nextButton___gH3HZ" title="Next page" rel="next">...</a>is not clickable at point (677, 23). Other element would receive the click:<input class="SearchBox-getty-module__input___VMxL0" data-testid="container-search-box-input" placeholder="Search the world’s best photos and images" aria-label="text" name="phrase" autocomplete="off" data-autosuggest-from="https://as.gettyservices.com:443/GettyImages.Autocomplete.KeywordService.Service/KeywordService1/Suggestedkeywords/{0}/{1}/{2}/{3}/{4}?usePopularity=true" value="lfc">
CSS选择器2
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > div.content_wrapper > section > div > main > div > div > div:nth-child(4) > div.Gallery-module__columnContainer___LqU0P > section > a > div'))).click()
Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<div class="PaginationRow-module__buttonText___XM2mA">...</div>is not clickable at point (673, 9). Other element would receive the click:<div class="SearchBox-getty-module__container___eqbmV" data-testid="container-search-box">...</div>
CSS选择器3
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > div.content_wrapper > section > div > main > div > div > div:nth-child(4) > div.Gallery-module__columnContainer___LqU0P > section > a > svg'))).click()
Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<svg xmlns="http://www.w3.org/2000/svg" width="8.233" height="13.861" viewBox="0 -4.861 8.233 13.861" class="PaginationRow-module__icon___daw1U">...</svg>is not clickable at point (703, 7). Other element would receive the click:<div class="SearchBox-getty-module__container___eqbmV" data-testid="container-search-box">...</div>
(Session info: chrome=103.0.5060.134)
CSS选择器4
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > div.content_wrapper > section > div > main > div > div > div:nth-child(4) > div.Gallery-module__columnContainer___LqU0P > section > a > svg > path'))).click()
Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<path d="M8.233 2.018L1.251 9 0 7.645l5.627-5.627L0-3.61l1.251-1.25 6.982 6.878z"></path>is not clickable at point (703, 7). Other element would receive the click:<div class="SearchBox-getty-module__container___eqbmV" data-testid="container-search-box">...</div>
(Session info: chrome=103.0.5060.134)
XPath 1
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/section/div/main/div/div/div[4]/div[2]/section/a'))).click()
Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<a class="PaginationRow-module__button___QQbMu PaginationRow-module__nextButton___gH3HZ" title="Next page" rel="next">...</a>is not clickable at point (677, 23). Other element would receive the click:<input class="SearchBox-getty-module__input___VMxL0" data-testid="container-search-box-input" placeholder="Search the world’s best photos and images" aria-label="text" name="phrase" autocomplete="off" data-autosuggest-from="https://as.gettyservices.com:443/GettyImages.Autocomplete.KeywordService.Service/KeywordService1/Suggestedkeywords/{0}/{1}/{2}/{3}/{4}?usePopularity=true" value="lfc">
XPath 2
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/section/div/main/div/div/div[4]/div[2]/section/a/div'))).click()
Error : selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
<div class="PaginationRow-module__buttonText___XM2mA">...</div>is not clickable at point (673, 9). Other element would receive the click:<div class="SearchBox-getty-module__container___eqbmV" data-testid="container-search-box">...</div>
XPath 3
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/section/div/main/div/div/div[4]/div[2]/section/a/svg'))).click()
Error: selenium.common.exceptions.TimeoutException: Message:
XPath 4
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/section/div/main/div/div/div[4]/div[2]/section/a/svg/path'))).click()
Error: selenium.common.exceptions.TimeoutException: Message:
解决方案
1. 使用精准且稳定的定位器
放弃绝对路径的CSS/XPath,改用元素的属性或语义化类名定位,避免页面结构变化导致定位失效:
# 利用title和rel属性定位 next_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[title="Next page"][rel="next"]'))) # 或者利用按钮的类名 next_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.PaginationRow-module__nextButton___gH3HZ')))
注意使用element_to_be_clickable代替visibility_of_element_located,确保元素不仅可见,还处于可点击状态。
2. 解决元素遮挡问题
错误提示显示搜索框遮挡了分页按钮,可通过以下方式处理:
- 滚动到按钮位置后点击:
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_btn) next_btn.click()
- 使用JavaScript直接点击:
driver.execute_script("arguments[0].click();", next_btn)
这种方式会绕过页面的元素层级遮挡,直接触发按钮的点击事件。
3. 排查链接格式的影响
两个链接的区别:
- 第一个是通用搜索入口,页面布局可能包含更多动态元素,容易出现遮挡
- 第二个是直接进入照片分类的结果页,带有排序、筛选参数,页面结构更稳定
建议测试第二个链接,验证分页按钮的定位和点击是否正常。如果第二个链接可以正常操作,说明第一个链接的页面布局导致了遮挡问题。
4. 确保页面完全加载
在操作前等待页面加载完成:
WebDriverWait(driver, 15).until(lambda d: d.execute_script('return document.readyState') == 'complete')
内容的提问来源于stack exchange,提问作者AnxiousDino

