Selenium 等待进度条 Python?

Selenium wait for progress bar Python?

我仍然无法使用 selenium 等待下一个代码的进度条

<div data-v-a5395076="" class="progress">
<div data-v-a5395076="" role="progressbar" aria-valuenow="98.4" 
aria-valuemin="0" aria-valuemax="100" class="progress-bar progress-bar-striped active" 
style="width: 98.4%;">98.4%</div>

这就是我要等到 100%
我无法获取文本或属性 我试图 get_attribute('aria-valuenow').text 但我认为不是这样。

while True:
            try:
                progress =  WebDriverWait(driver, 5).until(
                EC.presence_of_element_located((By.CSS_SELECTOR,".progress-bar.progress-bar-striped.active")))
                progressCondition =progress.get_attribute('aria-valuenow').text
                print(progressCondition)
                while True:
                    if progressCondition == '100':
                        break
                    else:
                        print(progress)
                        time.sleep(1)
                break
            except:
                print('Progress Not Found')
                time.sleep(1)
                timer += 1
                if timer > 30:
                    break
                else:
                    continue

怎么样?

我想这会有所帮助:

  1. presence_of_element_located 更改为 visibility_of_element_located 因为 presence_of_element_located returns Web 元素尚未完全呈现,仅存在于页面上,但仍然不可见visibility_of_element_located 等待更成熟的元素状态,当它已经可见时。
  2. progress.get_attribute('aria-valuenow').text
    中删除 .text get_attribute() 方法 returns 字符串值本身。而 .text 从 web 元素中提取文本。
    所以您的代码可能如下所示:
progress =  WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".progress-bar.progress-bar-striped.active")))
progressCondition =progress.get_attribute('aria-valuenow')
print(progressCondition)

因为 innerText/innerHTML 最终会变成 100 而不是 you need to induce WebDriverWait for the and you can use either of the following :

  • 使用 xpathinnerText:

    progress =  WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//div[@role='progressbar' and text()='100%']")))
    
  • 使用 xpatharia-valuenow 属性:

    progress =  WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//div[@role='progressbar' and @aria-valuenow='100%']")))