LoginSignup
yuni_
@yuni_

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Python SeleniumでWebページをスクロールしたい。

解決したいこと

Python SeleniumでWebページをスクロールしたい。

発生している問題・エラー

独学でpythonを勉強中です。
seleniumを使用し、yahooニュースで単語を検索→記事タイトルとURLを取得しようとしています。
単語を検索するところまではできたのですが、
スクロールするタイミングでエラーが出てしまいます。
エラー内容を調べたりしてみたのですが一向にわからず…。
よろしければお力をお貸しください。

該当のソースコード

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_argument('--incognito')

driver = webdriver.Chrome()

driver.implicitly_wait(10)

#yahooニュースで「python」と検索
driver.get('https://news.yahoo.co.jp/')
sleep(3)

search_box = driver.find_element(By.CSS_SELECTOR, "input.sc-1a5izri-3")
sleep(3)

search_box.send_keys('python')
sleep(3)

search_box.submit()
sleep(5)

#一番下までスクロールして「もっと見る」をクリック
while True:
    height = driver.execute_script('return document.body.scrollHeight')
    print(height)
    driver.execute_script(f'window.scrollTo(0,{height}')
    sleep(3)
    driver.find_element(By.CSS_SELECTOR, 'span.button')
    sleep(3)
    height2 = driver.execute_script('return document.body.scrollHeight')
    sleep(3)
    if height == height2:
        break

エラー文

DevTools listening on ws://127.0.0.1:60982/devtools/browser/e0223cc5-fdcc-45a6-9b8e-9047b65a0f78
Created TensorFlow Lite XNNPACK delegate for CPU.
7753
Traceback (most recent call last):
  File "C:\Users\tumug\anaconda3\envs\python-env\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tumug\anaconda3\envs\python-env\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\tumug\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\tumug\Desktop\lesson\putienshu83.py", line 30, in <module>
    driver.execute_script(f'window.scrollTo(0,{height}')
  File "c:\Users\tumug\Desktop\lesson\env\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 407, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\tumug\Desktop\lesson\env\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "c:\Users\tumug\Desktop\lesson\env\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: missing ) after argument list
  (Session info: chrome=124.0.6367.61)
Stacktrace:
        GetHandleVerifier [0x00007FF65917F922+3045234]
        (No symbol) [0x00007FF658E23652]
        (No symbol) [0x00007FF658CE7564]
        (No symbol) [0x00007FF658CED6A6]
        (No symbol) [0x00007FF658CEFD86]
        (No symbol) [0x00007FF658D79E63]
        (No symbol) [0x00007FF658D59DAA]
        (No symbol) [0x00007FF658D791C4]
        (No symbol) [0x00007FF658D59B53]
        (No symbol) [0x00007FF658D2813C]
        (No symbol) [0x00007FF658D28D71]
        GetHandleVerifier [0x00007FF6591AE89D+3237613]
        GetHandleVerifier [0x00007FF6591F0BA7+3508727]
        GetHandleVerifier [0x00007FF6591E976F+3478975]
        GetHandleVerifier [0x00007FF658F5F1C6+815126]
        (No symbol) [0x00007FF658E2ED0F]
        (No symbol) [0x00007FF658E29854]
        (No symbol) [0x00007FF658E299E2]
        (No symbol) [0x00007FF658E1A574]
        BaseThreadInitThunk [0x00007FFD3DC3257D+29]
        RtlUserThreadStart [0x00007FFD3F04AA48+40]


自分で試したこと

エラー内容を理解しようと調べてみましたが、該当する内容がみつかりませんでした。
よろしければ、ご教授お願い致します。

0

1Answer

-    driver.execute_script(f'window.scrollTo(0,{height}')
+    driver.execute_script(f'window.scrollTo(0,{height})')
1

Comments

  1. @yuni_

    Questioner

    完全に見落としておりました…!
    訂正したら無事に動きました。
    本当にありがとうございました!

  2. 解決であれば、当Q&Aをクローズしてください。

    なお、「もっと見る」をクリックするための、スクロールは不要だと思われます。

Your answer might help someone💌