the below is an example of controlling the play button. but how do i control the volume bar? my usual methods dont work.
this is what i have now. my usual methods of trying to hit the element is not working.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('/Users/Max/Desktop/Code/Selenium/chromedriver')
driver.get('https://www.youtube.com/watch?v=DXUAyRRkI6k')
for i in range(10):
print(i)
time.sleep(1)
player = driver.find_element_by_id('movie_player')
player.send_keys(Keys.SPACE)
time.sleep(1)
player.click()
CodePudding user response:
(1) For volume up/down, you can try ARROW_UP
and ARROW_DOWN
:
player.send_keys(Keys.ARROW_DOWN)
(2) For volume un-mute, you can try send_keys("m")
:
player.send_keys("m")
PS: Code below is untested but you could try it for clicking the volume icon.
The icon is called Mute (m)
but I don't know if it will respond to a Selenium click()
command...
testbtn = driver.find_element_by_xpath("//a[@title='Mute (m)']")
testbtn.click()
CodePudding user response:
You can do it with ActionChains()
:
sound_button = driver.find_element(By.CLASS_NAME, 'ytp-mute-button')
volume_panel = driver.find_element(By.CLASS_NAME, 'ytp-volume-panel')
actions = ActionChains(driver)
actions.move_to_element(sound_button)
actions.move_to_element(volume_panel)
actions.click_and_hold(volume_panel)
actions.move_to_element_with_offset(volume_panel, 20, 0)
actions.perform()
This sound bar can be moved from -20 to 20 using this line:
actions.move_to_element_with_offset(volume_panel, 20, 0)
So, for max volume it will be (volume_panel, 20, 0)
, for min volume it will be (volume_panel, -20, 0)
and you can set any volume between -20 and 20