错误“无法将元素<>滚动到视图中”

时间:2020-04-12 19:43:40

标签: python html selenium selenium-webdriver podcast

我正在使用硒设计刮板,以从Spreaker大规模下载播客片段。

# https://www.spreaker.com/show/alabamas-morning-news-with-jt


for i in range(3):
    print("Click number: {}".format(str(i)))
    see_more = browser.find_element_by_id("show episodes more")
    see_more.click()
    browserPage = bs4(browser.page_source, 'lxml')
    allEps.append( allEpisodesOnPage(browserPage) ) 

由于它们并非全部位于编号页面(/ episodes / page1,/ page2)上,因此我必须单击按钮以加载更多内容。

但是由于某些原因,我的代码找不到该按钮:

Traceback (most recent call last):
  File "KeepTalking__02.py", line 59, in <module>
    see_more.click()
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a id="show_episodes_more" class="btnn btnn_alternative btnn_small" href="#"> could not be scrolled into view

现在,“无法滚动到视图中”是Selenium中的标准错误,我被可能的解决方法所淹没:

  1. 将元素滚动到视图中。
  2. 等待该元素可见:

    element = WebDriverWait(browser, 10).until(
        ex_co.presence_of_element_located((By.CSS_SELECTOR, "#show_episodes_more")))
    
  3. 切换到其所在的框架。

但是由于某些原因,我仍然遇到完全相同的错误。是什么原因造成的?我拍了屏幕截图,并且按钮在页面上,所以我不知道错误是从哪里来的。

1 个答案:

答案 0 :(得分:0)

基本上,当其他WebElement叠加在WebElement上时,会发生ElementNotInteractableException。为了避免ElementNotInteractableException异常,您可以使用ActionChains,请参考以下解决方案:

select t1.* 
from Table_one t1 left join Table_two t2
on t2.id = t1.id and t2.name = t1.name
where t2.id is null

注意::请在您的解决方案中添加以下内容

element = WebDriverWait(browser, 10).until(
        ex_co.presence_of_element_located((By.CSS_SELECTOR, "#show_episodes_more")))

ActionChains(driver).move_to_element(element).click().perform()

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains