pythonseleniumselenium-webdriverwebdriverwaitautomation-testing

How can I wait for element attribute value with Selenium 4 n python?


Progress_Bar

I want to handle the progress bar to be stopped after a certain percent let's say 70%. So far I have got the solutions, they all are using .attributeToBe() method. But in selenium 4 I don't have that method present. How can I handle that?

This is the demo link - https://demoqa.com/progress-bar

I have tried to do that using explicit wait like others but I didn't find .attributrToBe() method in selenium 4. Is it possible to do that using loop?


Solution

  • You can use text_to_be_present_in_element_attribute expected_conditions.
    The following code works:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 60)
    
    url = "https://demoqa.com/progress-bar"
    driver.get(url)
    
    wait.until(EC.element_to_be_clickable((By.ID, "startStopButton"))).click()
    wait.until(EC.text_to_be_present_in_element_attribute((By.CSS_SELECTOR, '[role="progressbar"]'),"aria-valuenow","70"))
    wait.until(EC.element_to_be_clickable((By.ID, "startStopButton"))).click()
    

    UPD
    For the second page the following code works as well:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 60)
    
    url = "http://uitestingplayground.com/progressbar"
    driver.get(url)
    
    wait.until(EC.element_to_be_clickable((By.ID, "startButton"))).click()
    wait.until(EC.text_to_be_present_in_element_attribute((By.CSS_SELECTOR, '[role="progressbar"]'),"aria-valuenow","75"))
    wait.until(EC.element_to_be_clickable((By.ID, "stopButton"))).click()