Selenium - 找不到 youtube 搜索栏
Selenium - cannot find youtube search bar
错误信息:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: input.ytd-searchbox
我一直收到此错误,即使我从其他解决方案中添加了睡眠命令以使用 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()
错误信息:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: input.ytd-searchbox
我一直收到此错误,即使我从其他解决方案中添加了睡眠命令以使用 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()