【问题标题】:How can I click the "ABOUT" button on a YouTube channel page?如何点击 YouTube 频道页面上的“关于”按钮?
【发布时间】:2021-09-04 19:54:45
【问题描述】:

我正在尝试点击 YouTube 频道页面上的“关于”按钮。

但是,在我运行以下代码后,Chrome 并没有显示“关于”信息。

! pip install selenium
from selenium import webdriver

! pip install beautifulsoup4
from bs4 import BeautifulSoup

import time
import requests
import random
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
keyword=['"K_Pop TV"']

for kk in keyword:
    driver.get("http://youtube.com")
    #enter 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
    driver.find_element_by_css_selector('div#avatar.style-scope.ytd-channel-renderer').click()
    time.sleep(2)
    #click "ABOUT"
    driver.find_element_by_css_selector('tp-yt-paper-tab.style-scope.ytd-c4-tabbed-header-renderer').click()

Chrome 在 YouTube 频道页面的“HOME”暂停。它没有继续显示“关于”。请帮帮我。

【问题讨论】:

    标签: python selenium xpath css-selectors webdriver


    【解决方案1】:

    试试下面的xpath:

    //div[contains(text(),'About')]
    

    然后像下面这样点击它:

    driver.find_element_by_xpath("//div[contains(text(),'About')]").click()
    

    【讨论】:

      【解决方案2】:

      您使用的定位器不是唯一的。
      试试这个:

      driver.find_element_by_xpath('(//div[@class="tab-content style-scope tp-yt-paper-tab"])[last()]').click()
      

      它是独一无二的,不是基于也可以改变的文本

      【讨论】:

      • 这里使用类的任何具体原因?因为我个人认为它可能会改变,而 About` 文本永远不会改变
      • @cruisepandey 类名非常稳定,而文本可能因用户区域设置语言而异。例如,我在那里看到了一些其他语言/文本:)
      • 好吧,我以为 OP 会在 en-US 上运行
      猜你喜欢
      • 2019-06-21
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      相关资源
      最近更新 更多