Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用selenium和python实现Web自动化-打开youtube视频链接_Python_Selenium_Web_Automation - Fatal编程技术网

使用selenium和python实现Web自动化-打开youtube视频链接

使用selenium和python实现Web自动化-打开youtube视频链接,python,selenium,web,automation,Python,Selenium,Web,Automation,我想打开一个youtube视频。导航到搜索结果后,我无法打开所需的链接。它不接受searchBaari的xpath。任何帮助都将不胜感激 代码如下: from selenium import webdriver driver = webdriver.Chrome('/Users/souravroy/Downloads/chromedriver-2') driver.get('https://youtube.com') searchbox = driver.find_element_by_x

我想打开一个youtube视频。导航到搜索结果后,我无法打开所需的链接。它不接受searchBaari的xpath。任何帮助都将不胜感激

代码如下:

from selenium import webdriver

driver = webdriver.Chrome('/Users/souravroy/Downloads/chromedriver-2')

driver.get('https://youtube.com')

searchbox = driver.find_element_by_xpath('//*[@id="search"]')

searchbox.send_keys('Baari')

searchButton = driver.find_element_by_xpath('//*[@id="search-icon-legacy"]/yt-icon')

searchButton.click()

driver.implicitly_wait(4500)

searchBaari = driver.find_element_by_xpath('//*[@id="Baari Lyrics Translation"]/yt-formatted-string')

searchBaari.Click()

我没有在页面上找到任何带有
@id=“Baari歌词翻译”
的元素。虽然有一个链接与这样的标题部分匹配。尝试修改最后几行以使用此定位器:

searchBaari = driver.find_element_by_xpath('//a[contains(@title,"Baari Lyrics Translation")]')
searchBaari.click()
这应该会成功的,我希望它能有所帮助。祝你好运。

id是
@id=“video title”
。但问题是,它将打开搜索结果的第一个视频。