python 中的selenium:ElementNotInteractableException - 元素无法滚动到视图中(但确实如此)

问题描述 投票:0回答:1

我在 python 中有以下代码:它在指定页面上打开 Firefox,关闭“接受 cookies”窗口,然后向下滚动以单击“Carica Altro”按钮。当我点击按钮时,我得到:

ElementNotInteractableException: Message: Element <button class="button button--primary medium expanded button--login" type="submit"> could not be scrolled into view
但实际上它是可见的,因为我在用户界面中看到它(如图所示)。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.action_chains import ActionChains
import time

my_url='https://www.raiplay.it/programmi/winxclub/episodi/stagione-5'

def main():
  options = FirefoxOptions()
  browser = webdriver.Firefox(options=options)
  browser.implicitly_wait(10)
  browser.get(my_url)
  wait = WebDriverWait(browser, 30)
  try:
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "as-oil__btn-primary.as-js-optin"))).click()
    print('Clicked successfully')
  except:
    print('Could not click')
    pass

  l =browser.find_element(By.CLASS_NAME,"button.button--primary.medium.expanded")
  browser.execute_script('window.scrollBy(0, 1500)') 

  time.sleep(10)
  l.click()
  return
python selenium-webdriver web-scraping
1个回答
0
投票

您可以使用 https://github.com/seleniumbase/SeleniumBase 来解决您的问题并简化您的脚本。

pip install seleniumbase
,然后运行
python
:

from seleniumbase import SB

with SB() as sb:
    sb.open("https://www.raiplay.it/programmi/winxclub/episodi/stagione-5")
    sb.click('button[data-qa="oil-YesButton"]')
    sb.scroll_to_bottom()
    sb.sleep(0.5)
    sb.scroll_to('a[title="Carica altro"]')
    sb.sleep(0.5)
    sb.click('a[title="Carica altro"]')

    breakpoint()

要从

breakpoint()
继续,请输入
c
,然后按
Enter

© www.soinside.com 2019 - 2024. All rights reserved.