title: How to search and play a video on YouTube using Selenium in Python?
body: <p>I want to search and play a video using Selenium in Python.<br />
  Here is the code:</p>
  <pre class="lang-py prettyprint-override"><code>import time
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  
  driver = webdriver.Chrome()
  driver.maximize_window()
  driver.get(www.youtube.com)
  
  wait = WebDriverWait(driver, 3)
  presence = EC.presence_of_element_located
  visible = EC.visibility_of_element_located
  
  # Search for the video.
  time.sleep(1)
  wait.until(visible((By.NAME, &quot;search_query&quot;)))
  driver.find_element_by_name(&quot;search_query&quot;).click()
  driver.find_element_by_name(&quot;search_query&quot;).send_keys(video)
  driver.find_element_by_id(&quot;search-icon-legacy&quot;).click()
  
  # Play the video.
  wait.until(visible((By.ID, &quot;video-title&quot;)))
  driver.find_element_by_id(&quot;video-title&quot;).click()
  </code></pre>
  <p>But the problem is, it plays the first video on the home page before it completes searching.<br />
  Can you guys suggest how to play the video?</p>