How to wait and get the value of a Span object in Selenium Python binding
I have code on my web page.
<div id="" class="user_acc_setails"> <ul id="accDtlUL"> <li>First Name: <span id="f_name">Anuja</span></li>
By the time the page loads, the value for Sapn is not set. It takes very little time to set the value. I want to wait and get this value in my Python file.
I am currently using the following code,
element = context.browser.find_element_by_id('f_name') assert element.text == 'Anuja'
But that gives me an AssetionError . How can i solve this?
thanks
Correct, in this case it would be to use Explicit Expectation (see Python code here). So you need something like
from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.text_to_be_present_in_element((By.Id,'f_name'), 'Anuja'))
here is the function that does this:
def wait_on_element_text(self, by_type, element, text): WebDriverWait(self.driver, 10).until( ec.text_to_be_present_in_element( (by_type, element), text) )
If the by_type parameter replaces, for example, By.XPATH, By.CSS_SELECTOR. Where the element is replaced with the element path - xpath elements, unique selector, id, etc. Where the text is replaced by the text of the element, for example. The string associated with the item.
url = "http://..." driver = webdriver.Firefox() driver.get(url) wait = WebDriverWait(driver, 10) try: present = wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, "myclassname"), "valueyouwanttomatch")) elem = driver.find_element_by_class_name("myclassname") print elem.text finally: driver.quit()
I realized that the wait.until
returned object wait.until
not an element, but a boolean, so I need to call the locate element again.