1

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

Hemanthsai
  • 35
  • 4

1 Answers1

0

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()
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • This time the chrome is loading and the YouTube is also loading and in the search bar it is typing "lucasfilm" and then again the chrome is shutting down. – Hemanthsai Apr 09 '23 at 16:43
  • So, you're saying it's doing exactly what the script says? I don't understand the issue. The code is working as intended. – JeffC Apr 09 '23 at 21:34
  • It does the same until it fills the name in the search box and after that, rather than showing the results, the chrome is shutting down. – Hemanthsai Apr 10 '23 at 03:33
  • That's because of `driver.quit()`. If you don't need it, you can delete it or push it to later. It's just to close the browser at the end of the script. – JeffC Apr 10 '23 at 05:27
  • I have tried that bro. The problem still persists. Problem Demo-video : [link](https://drive.google.com/file/d/1VzBSITa-Yy70rTuc3iqa1O1BD1jz2geY/view?usp=sharing) – Hemanthsai Apr 10 '23 at 10:46
  • It's the end of the script. It apparently automatically closes the browser the way you run the test. You'll notice that when you ran your script, it was the exact same behavior. I guess I don't understand what the issue is. – JeffC Apr 10 '23 at 14:53
  • How do I keep my browser running? – Hemanthsai Apr 10 '23 at 15:48
  • There are several options listed [here](https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open) – JeffC Apr 10 '23 at 16:55