CanaryTheBird
CanaryTheBird

Reputation: 259

How can I click first channel in YouTube with python after searching one?

I am trying to click the first YouTube channel on the top of the result page. My code is as follows.

from selenium import webdriver
!pip install webdriver-manager
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
keyword=['"K_Pop TV"']

for kk in keyword:
    driver.get("http://youtube.com")
    #input keyword
    driver.find_element_by_name("""search_query""").send_keys(kk)
    #search
    driver.find_element_by_id("search-icon-legacy").click()
    time.sleep(2)
    #click the first channel on result page
    driver.find_elements_by_id('avatar').click()

However, I kept showing the error of "'list' object has no attribute 'click'", which may mean that it couldn't find the element of the channel access. Would someone please help me?

Upvotes: 2

Views: 228

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

This error 'list' object has no attribute 'click is cause you are using find_elements that will return a list of web element.

A list in Python does not have a .click() method

use find_element instead which will return a single web element.

so make the below change in your existing code :

driver.find_element_by_id('avatar').click()

Upvotes: 1

Related Questions