gpt4 book ai didi

python - Selenium -无法找到youtube搜索栏

转载 作者:行者123 更新时间:2023-12-03 05:28:32 25 4
gpt4 key购买 nike

错误消息:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: input.ytd-searchbox

即使我从其他解决方案中添加了sleep命令以使页面使用javascript动态加载,我仍会收到此错误,但仍然找不到它?

import time
from selenium import webdriver
firefox = webdriver.Firefox()
firefox.get("https://www.youtube.com")
element = firefox.find_element_by_css_selector("ytd-mini-guide-entry-renderer.style-scope:nth-child(3) > a:nth-child(1)") # opens subscriptions
element.click()
time.sleep(10) # wait for page to load before finding it
searchelement = firefox.find_element_by_css_selector('input.ytd-searchbox') # search bar

searchelement.send_keys("Cute Puppies")
searchelement.submit()

最佳答案

我刚刚更改了CSS选择器。您在这里做错了。
嗯...我是怎么做到的?好了,选择CSS选择器有一个简单的技巧。

  • 首先输入标签名称。您的情况是input
  • 如果此处存在ID,请键入带有#的ID名称。所以
    像我一样:#search
  • 如果那里有一个class,请在名称前使用.。对于
    示例.search

  • 试试这个。工作正常:
    import time
    from selenium import webdriver
    firefox = webdriver.Firefox(executable_path=r'C:\Users\intel\Downloads\Setups\geckodriver.exe')
    firefox.get("https://www.youtube.com")
    element = firefox.find_element_by_css_selector(".style-scope:nth-child(1) > #items > .style-scope:nth-child(3) > #endpoint .title") # opens subscriptions
    element.click()
    time.sleep(10) # wait for page to load before finding it
    searchelement = firefox.find_element_by_css_selector('input#search') # search bar

    searchelement.send_keys("Cute Puppies")
    searchelement.submit()

    关于python - Selenium -无法找到youtube搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61345666/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com