1

I want to make a search on youtube. The page opens, clicks to search, but I get an error when it comes to writing.

OpenQA.Selenium.ElementNotInteractableException: 'element not interactable
var chromeDriverService = ChromeDriverService.CreateDefaultService();
            chromeDriverService.HideCommandPromptWindow = true;
            IWebDriver driver;
            ChromeOptions options = new ChromeOptions();
            options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
            options.AddArgument("start-maximized");
            options.AddArgument("disable-infobars");
            options.AddArgument("--disable-extensions");
            driver = new ChromeDriver(chromeDriverService, options);
            driver.Navigate().GoToUrl("https://www.youtube.com/");
            IWebElement search = driver.FindElement(By.Id("search-form"));
            search.Click();
            WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
            search.SendKeys("xyz");
Mesut Bla
  • 127
  • 1
  • 7
  • I understand your question is directed at sending keys to the search box, but as a work around could you send the parameter in the query? For example driver.Navigate().GoToUrl("https://www.youtube.com/results?search_query=hello"); By the way I didn't want to post that previous answer as an answer, it's just at the time I literally didn't have enough rep to comment (50 rep), I just now gained that ability. – Michael Aug 29 '20 at 06:34
  • I can type url search is not what I want. After entering youtube, it will perform the search I want. – Mesut Bla Aug 29 '20 at 06:38
  • Does this answer your question? [element not interactable exception in selenium web automation](https://stackoverflow.com/questions/45183797/element-not-interactable-exception-in-selenium-web-automation) – CoolBots Aug 29 '20 at 06:58
  • no i tried but it didn't work – Mesut Bla Aug 29 '20 at 07:01
  • You tried the delay method described in that answer? Note,`Thread.Sleep` is *not the same*. As posted, your question is a duplicate; if you could please update your code based on the duplicate answer *and explain what didn't work*, I'll retract my vote to close this question as duplicate. Thank you! – CoolBots Aug 29 '20 at 07:04
  • error still i edited the same question. – Mesut Bla Aug 29 '20 at 07:40
  • I added an answer, as I realized there are several issues with your approach, which makes the other question not an exact duplicate. Also, it's in java, so there is a bit of translation to C# Selenium driver. – CoolBots Aug 29 '20 at 07:54

1 Answers1

1

There are several issues with your approach:

  • The element you're looking for By.Id should be "search", not "search-form"
  • There are 4 elements on youtube.com home page that have id of "search"
  • The page might not fully load when you're accessing the elements. Your use of WebDriverWait is also incorrect.

Solution:

  • Find all elements with id of "search" once they are available, and grab the input element:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
options.AddArgument("start-maximized");
options.AddArgument("disable-infobars");
options.AddArgument("--disable-extensions");
            
var driver = new ChromeDriver(chromeDriverService, options);
driver.Navigate().GoToUrl("https://www.youtube.com/");

// New code - get the input element with id "search" once it's ready            
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(1500));            
var elementsWithSearchID = wait.Until((driver) => driver.FindElements(By.Id("search")));
var search = elementsWithSearchID.Where(e => e.TagName == "input").FirstOrDefault();

// No need to click it - just send text with a `\n`, 
// which simulates typing the text and pressing the enter key
search.SendKeys("Hello\n");
CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • 1
    So how can I catch a channel match in search results? For example, click the video with the channel name "Khans Lyrics" in the results. – Mesut Bla Aug 29 '20 at 08:28
  • @MesutBla Please give it a shot on your own, and if you have questions that you can't resolve, please ask a new question on StackOverflow. Please make sure to post relevant details, and explain what you tried, and what the issue is. Thank you! – CoolBots Aug 29 '20 at 08:32
  • 1
    I got it, you are right. thank you so much. I'm dealing with it, if I can't solve it, I'll open a new topic. – Mesut Bla Aug 29 '20 at 08:36