我如何用Selenium 4 n python等待元素属性值?

x33g5p2x  于 2022-12-13  发布在  Python
关注(0)|答案(1)|浏览(124)

我想处理进度条在达到某个百分比后停止,比如说70%。到目前为止,我已经得到了解决方案,它们都使用了.attributeToBe()方法。但是在Selenium 4中,我没有这个方法。我该怎么处理呢?
这是演示链接-https://demoqa.com/progress-bar
我已经尝试过像其他人一样使用显式等待来实现这一点,但是我在Selenium 4中没有找到. attributToBe()方法。是否可以使用循环来实现这一点?

mzmfm0qo

mzmfm0qo1#

您可以使用text_to_be_present_in_element_attributeexpected_conditions
下面的代码是有效的:

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

对于第二个页面,以下代码同样有效:

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()

相关问题