I'm trying to access the select element on this page: https://yt5s.com/en133?q=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSBjQ9tuuTJQ
I'm using the following code to access the dropdown:
driver.findElement(By.xpath("//select[@class='form-control form-control-small hidden']/optgroup[@label='mp3']")).click();
But I receive the following error
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='form-control form-control-small hidden']/optgroup[@label='mp3']"}
The following answers in this chain are all correct. I found the issue to be that my code opened the above url in a new tab and I didn't switch to this new tab yet. The following piece of code let me switch to the new tab and now it is working.
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.close();
driver.switchTo().window(tabs.get(1));