How can I click the "SORT BY" button and select "Newest first" item on YouTube video page?


As a beginner to Python, I’ve been trying to click the "SORT BY" button and select "Newest first" item on YouTube video page (Like the following picture shows).
enter image description here
I tried with the following code.

! pip install selenium
from selenium import webdriver
import time
driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
!pip install pynput
from pynput.mouse import Button, Controller
mouse = Controller()
mouse.scroll(0, -5)
#click "SORT BY" button
#click "Newest first" button

However, driver.find_element('').click() and driver.find_elements_by_css_selector('#dropdown').click() do not work. It kept showing the error of Message: invalid argument: invalid locator .
If anyone knows how to solve it, please help me.


try below code :

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ""))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Newest first']"))).click()

Imports :

from import WebDriverWait
from import By
from import expected_conditions as EC

Answered By – cruisepandey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published