如何始终 select 使用 selenium 在 youtube 视频上提供最高质量
How to always select the highest available quality on youtube video with selenium
driver.find_element(By.CSS_SELECTOR, value='button.ytp-button.ytp-settings-button').click()
time.sleep(1)
driver.find_element(By.XPATH, value="/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[1]/div/div/div/ytd-player/div/div/div[30]/div/div/div[3]").click()
有些视频有最高质量的 1080p,有些有 1440p,有些有 4k 等等,我如何让 selenium 点击最高的。
我正在考虑做一些事情,比如将所有可用的品质附加到列表中,然后只使用列表[0] 或列表[-1]。取决于最高可用质量采用什么索引。
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'2160p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'1440p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'1080p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'720p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'480p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'360p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'240p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'144p')]").click()
except NoSuchElementException:
print("App has crashed") # Do not think code can reach here, but just in case
exit()
这个应该不错吧,如果有什么“更快”的不胜感激
如果您希望设置最大可用分辨率(或最高分辨率),则无需依赖定义分辨率的字符串。只需单击存在的第一个按钮。另外,您发布的答案不是一个好习惯(改用循环)
driver.find_element(By.XPATH, value='(//div[@class="ytp-menuitem"])[1]').click()
driver.find_element(By.CSS_SELECTOR, value='button.ytp-button.ytp-settings-button').click()
time.sleep(1)
driver.find_element(By.XPATH, value="/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[1]/div/div/div/ytd-player/div/div/div[30]/div/div/div[3]").click()
有些视频有最高质量的 1080p,有些有 1440p,有些有 4k 等等,我如何让 selenium 点击最高的。
我正在考虑做一些事情,比如将所有可用的品质附加到列表中,然后只使用列表[0] 或列表[-1]。取决于最高可用质量采用什么索引。
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'2160p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'1440p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'1080p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'720p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'480p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'360p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'240p')]").click()
except NoSuchElementException:
try:
driver.find_element(By.XPATH, value="//span[contains(string(),'144p')]").click()
except NoSuchElementException:
print("App has crashed") # Do not think code can reach here, but just in case
exit()
这个应该不错吧,如果有什么“更快”的不胜感激
如果您希望设置最大可用分辨率(或最高分辨率),则无需依赖定义分辨率的字符串。只需单击存在的第一个按钮。另外,您发布的答案不是一个好习惯(改用循环)
driver.find_element(By.XPATH, value='(//div[@class="ytp-menuitem"])[1]').click()