Search code examples
htmlpython-3.xselenium-webdriverwebautomation

can someone explain why there is a sudden shutdown of chrome?


When I run my chrome automation programme using selenium and webdriver

the chrome opens and suddenly closes

here is my code:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.youtube.com/")
searchbox = driver.find_element('xpath', '//*[@id="search-input"]')
searchbox.send_keys("animeballsdeep")

searchButton = driver.find_element('xpath', '//*[@id="search-icon-legacy"]')
searchButton.click()

When I run my code

Output:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    searchbox.send_keys("animeballsdeep")
  File "/opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 231, in send_keys
    self._execute(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 404, in _execute
    return self._parent.execute(command, params)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=112.0.5615.49)
Stacktrace:
0   chromedriver                        0x0000000100e01670 chromedriver + 4298352
1   chromedriver                        0x0000000100df9bbc chromedriver + 4266940
2   chromedriver                        0x0000000100a2c5dc chromedriver + 280028
3   chromedriver                        0x0000000100a60cac chromedriver + 494764
4   chromedriver                        0x0000000100a60340 chromedriver + 492352
5   chromedriver                        0x0000000100a5bb90 chromedriver + 474000
6   chromedriver                        0x0000000100aa0080 chromedriver + 753792
7   chromedriver                        0x0000000100a5a2d0 chromedriver + 467664
8   chromedriver                        0x0000000100a5b354 chromedriver + 471892
9   chromedriver                        0x0000000100dc16c4 chromedriver + 4036292
10  chromedriver                        0x0000000100dc5c64 chromedriver + 4054116
11  chromedriver                        0x0000000100dcc2d8 chromedriver + 4080344
12  chromedriver                        0x0000000100dc6970 chromedriver + 4057456
13  chromedriver                        0x0000000100d9d8dc chromedriver + 3889372
14  chromedriver                        0x0000000100de525c chromedriver + 4182620
15  chromedriver                        0x0000000100de53b4 chromedriver + 4182964
16  chromedriver                        0x0000000100df40f4 chromedriver + 4243700
17  libsystem_pthread.dylib             0x000000019dae7fa8 _pthread_start + 148
18  libsystem_pthread.dylib             0x000000019dae2da0 thread_start + 8

It shows like message : element not interactable


Solution

  • You were very close... the problem was that your first locator was actually targeting the DIV surrounding the search INPUT and not the search INPUT itself.

    <div id="search-input" class="ytd-searchbox-spt" slot="search-input">
             ^^^^^^^^^^^^
        <input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Search" aria-label="Search" role="combobox" aria-haspopup="false" aria-autocomplete="list" dir="ltr" class="ytd-searchbox" style="outline: none;">
    </div>
    

    The ID you want is "search".

    Updated code is below. I added some waits just to be safe.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.youtube.com")
    
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys('lucasfilm')
    wait.until(EC.element_to_be_clickable((By.ID, "search-icon-legacy"))).click()
    
    driver.quit()