硒-无法找到youtube搜索栏

三角洲

错误信息 :

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()
阿比·萨尔维(Abhay Salvi)

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

  1. 首先输入标签名称。您的情况是input
  2. 如果这里有ID礼物,请在上面输入ID名称#正如我那样:#search
  3. 如果有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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章