简体   繁体   中英

expected_conditions with Selenium in Python

I want to load my driver until the text of an element become numbers:

here is the element:

<span id="viewed">-<span> or <span id="viewed"><span>

it will become :

<span id="viewed">12345<span>

any solution?

This is untested:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

ints = "012345679"

def custom_ec(driver):
    ''' Custom expected condition function to feed wait.until
    '''
    elem_text = driver.find_element_by_id("viewed").text

    # Test to see if all of the values are ints
    if all(map(lambda x: x in ints, elem_text)):
        # If all of the char in the span are ints, return the value as an int
        return int(elem_text)
    else:
        return False

driver = webdriver.Chrome()
try:
    driver.get("http://your.url/here")
    int_value = WebDriverWait(driver, 30).until(custom_ec)
finally:
    driver.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM