0

Error: Youtube opens and closes without executing the operations. Is there any way we can place chromedriver in selenium file or any other way out.

I tried this .:

import selenium from selenium import webdriver from selenium.webdriver.common.by import By import time url='https://youtube.com' driver=webdriver.Chrome() driver.get(url) searchbox=driver.find_element(By.XPATH,'//*[@id="search"]') searchbox.send_keys("Google") searchbutton=driver.find_element(By.XPATH,'//*[@id="search-icon-legacy"]/yt-icon/yt-icon- shape/icon-shape/div') searchbutton.click() time.sleep(25) driver.quit() 

    2 Answers 2

    1

    Open YouTube in Chrome and use the developer console (inspect element) to view the HTML. Hit CTRL+F to open the Find bar and enter the XPATH you're using for the search bar (//input[@id="search"]). You will see that that three elements are returned! The id of "search" is not unique. Selenium's find_element() returns the first element, which is NOT the search box. So in the XPATH you need to specify you're looking for an <input> element.

    The other selector will not work either as-is. Here are correct locators:

    searchbox = driver.find_element(By.XPATH,'//input[@id="search"]') searchbox.send_keys("Google") driver.find_element(By.ID, 'search-icon-legacy').click() 
      0

      When you launch a browser using selenium, very likely you will get Accept/Reject cookies pop-up. Assuming you are getting this pop-up, here is the working code.

      from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time url='https://youtube.com' driver=webdriver.Chrome() driver.maximize_window() driver.get(url) wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label,'Accept')]"))).click() time.sleep(5) wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='search']"))).send_keys("Google") wait.until(EC.element_to_be_clickable((By.ID, "search-icon-legacy"))).click() time.sleep(25) driver.quit() 

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.