title: Twitch Chat box in selenium
body: <p>Well I pieced together a script with multiple google searches which is based on selenium,<strong>the intention of this code is to try to type in the chat box of twitch with a randomized message automatically(not limited use to only my channel)</strong>, but it fails miserably. I have a general knowledge on python but that's it, which is why ended up here since I couldn't find the answer myself.</p>
  
  <pre><code>from selenium import webdriver
  from random import randint
  import keyboard
  browser = webdriver.Chrome(executable_path=r'C:\Python27\Scripts\chromedriver')
  browser.get('https://www.twitch.tv/alexaki99')
  #getting chat box
  chat=browser.find_element_by_css_selector('#root &gt; div &gt; div &gt; div.tw-full-height.tw-flex.tw-flex-column.tw-flex-nowrap.tw-relative &gt; div &gt; div.right-column.tw-full-height.tw-flex-shrink-0.tw-relative &gt; div &gt; div &gt; div.tw-full-width.tw-full-height.tw-z-default.tw-flex.tw-flex-column.tw-flex-grow-1.tw-flex-nowrap.tw-relative &gt; div.tw-full-width.tw-full-height.tw-overflow-hidden.tw-flex.tw-flex-column.tw-flex-grow-1.tw-flex-shrink-1 &gt; div &gt; div.chat-input.tw-pd-b-2.tw-pd-x-2 &gt; div.tw-z-default.tw-relative &gt; div:nth-child(1) &gt; textarea')
  time.sleep(40) #manually logging in because of captcha
  print 'done sleep' #verification that the delay
  chat.click()
  while True:
      num=(randint(0, 7))
      if num==1:
          chat.send_keys('1')
      if num==2:
          chat.send_keys('2')
      if num==3:
          chat._send_keys('3')
      if num==4:
          chat._send_keys('4')
      if num==5:
          chat._send_keys('5')
      if num==6:
          chat._send_keys('6')
      if num==7:
          chat._send_keys('7')
      if keyboard.is_pressed('shift+q'):
          break
  driver.quit()
  </code></pre>
  
  <p>The error I get goes as follows:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Alex\Desktop\twitch_auto.py", line 11, in &lt;module&gt;
      chat.click()
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
      self._execute(Command.CLICK_ELEMENT)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
      return self._parent.execute(command, params)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
      self.error_handler.check_response(response)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
      raise exception_class(message, screen, stacktrace)
  StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
    (Session info: chrome=65.0.3325.146)
    (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
  </code></pre>
  
  <p>I'm aware I could use irc twitch but I got lost quick,so I stayed with what I had.</p>
  
  <p>Thanks in advance.</p>