Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reduce volume slider to 0%
#1
I am trying to figure out how to reduce the volume slider to 0% as it starts somewhere are 80%-ish and loud and annoying when testing.

volume slider html:
<div class="controls-group control-bar inflexible" data-reactid=".0.0.3.2.1.2">
 <div class="btn cs-button volume " data-reactid=".0.0.3.2.1.2.0">
  <button class="btn-unstyled text-align-middle" data-reactid=".0.0.3.2.1.2.0.0" style="zoom:100.5%;transform:translate(0px, 0px) scale(0.9950248756218907) translate(0px, 0px) ;">
   <svg class="cs-icon" data-reactid=".0.0.3.2.1.2.0.0.0" height="15" style="zoom:100.5%;transform:translate(0px, 0px) scale(0.9950248756218907) translate(0px, 0px) ;" viewbox="0 0 15 15" width="15">
    <use class="cs-icon-shadow" transform="translate(0, 1)" xlink:href="#player-volume" xmlns:xlink="http://www.w3.org/1999/xlink">
    </use>
    <g id="player-volume">
     <polygon points="4.3,10.7 0,10.7 0,4.3 4.3,4.3 8.6,0 8.6,15">
     </polygon>
     <rect class="" height="6" width="1" x="10" y="5">
     </rect>
     <rect class="" height="8" width="1" x="12" y="4">
     </rect>
     <rect class="hidden" height="10" width="1" x="14" y="3">
     </rect>
    </g>
   </svg>
   <span class="accessibility" data-reactid=".0.0.3.2.1.2.0.0.1">
    volume
   </span>
  </button>
  <div class="btn cs-volume cs-button slider-bar hidden " data-reactid=".0.0.3.2.1.2.0.1" id="control-volume-slider" style="zoom:100.5%;transform:translate(0px, 0px) scale(0.9950248756218907) translate(0px, 0px) ;">
   <input aria-orientation="vertical" class="cs-volume" data-reactid=".0.0.3.2.1.2.0.1.0" max="1.0" min="0" step="0.1" type="range" value="0.8"/>
  </div>
 </div>
</div>
If you click the volume button the slider bar appears above it. The element of the actual slider bar is
<div class="btn cs-volume cs-button slider-bar  " style="zoom:100.5%;transform:translate(0px, 0px) scale(0.9950248756218907) translate(0px, 0px) ;" id="control-volume-slider" data-reactid=".0.0.3.2.1.2.0.1"><input class="cs-volume" type="range" min="0" max="1.0" value="0.8" step="0.1" aria-orientation="vertical" data-reactid=".0.0.3.2.1.2.0.1.0"></div>
This is my code for attempting to slide that slider bar down to 0%
            
    def set_volume(self):
        btn = self.browser.find_element_by_xpath('//*[@id="main-window"]/div[2]/div[1]/div/button')
        btn.click() #click volume button to enable slider control
        
        slider_class = 'btn cs-volume cs-button slider-bar hidden '
        slider = self.browser.find_element_by_id('control-volume-slider')
        height = slider.size['height']
        width = slider.size['width']
        move = ActionChains(self.browser)
        percent = 100
        
        if width > height:
            direction_offset = width
        else:
            direction_offset = width
        move.click_and_hold(slider).move_by_offset(percent * direction_offset / 100, 0).release().perform()
I am not sure if there is a method of selenium to just set the id value to 0.0 (or maybe there is a javascript method) or if you have to manually slide the bar down. I attempted the latter, but i cannot seem to figure the proper method out?
Recommended Tutorials:
Reply
#2
Hi,

what about setting the "value" property to 0? It's currently set to 0.8, which is guess is 80% of the volume?

Regards, noisefloor
Reply
#3
This work for me,testing local so local3.html is copy of your code.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time
from bs4 import BeautifulSoup

#--| Setup
options = Options()
#options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe')
#--| Parse or automation
browser.get('file:///E:/div_code/scrape/local3.html')
slider =  browser.find_elements_by_css_selector('#control-volume-slider > input')[0]
move = ActionChains(browser)

# Move to 0% after 5-sec
time.sleep(5)
move.click_and_hold(slider).move_by_offset(-100, 1).release().perform()
Reply
#4
I cant seem to even get the local to run at all
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time
from bs4 import BeautifulSoup
 
#--| Setup
options = Options()
#options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=r'/home/metulburr/chromedriver')
#--| Parse or automation
browser.get('/home/metulburr/local3.html')
slider =  browser.find_elements_by_css_selector('#control-volume-slider > input')[0]
move = ActionChains(browser)
 
# Move to 0% after 5-sec
time.sleep(5)
move.click_and_hold(slider).move_by_offset(-100, 1).release().perform()
give the error
Error:
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot navigate to invalid URL"}
Switching the code in the actual site...
It doesnt seem to work?
It closes the slider view from the volume button.

So i assumed that -100 was too much and maybe it hit the volume button to close it again, but after trying smaller integers, the same effect occurs.
Recommended Tutorials:
Reply
#5
For testing local you need address to file in browser.
To get it drag file into address bar of browser.
So eg for Linux it would look like this:
file:///home/tom/Documents/py_files/local3.html
Reply
#6
ah ok i didnt know the path needed to be preceeded with file:

So it works in the local version, but not on the website. However on the website, the slider only appears when you click the volume button. And after this change it disappears and does not change the volume.
Recommended Tutorials:
Reply
#7
As you know it's difficult to give advice without testing this live on web-site.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using range slider in flask webpage to use in python KimPet 2 7,527 Jan-23-2021, 11:58 PM
Last Post: snippsat
  I am having a problem with my Slider Hadad 5 4,826 Jul-24-2019, 01:36 PM
Last Post: Hadad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020