pythonhtmlseleniumrefreshzero-width-space

Python, selenium - how to refresh page until item found?


I am trying to refresg page until item appears but my code doesn't work (I took pattern on that: python selenium keep refreshing until item found (Chromedriver)).

Here is the code:

while True:
    try:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                break
    except NoSuchElementException:
        driver.refresh
    else:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                h1.click()
                break
        break

These fragment is because I have to find one item with the same class name and replace BOM with "" (find_element_by_partial_link_text didn't work).

for h1 in driver.find_elements_by_class_name("name-link"):
    text = h1.text.replace('\uFEFF', "")
    if "Puffy" in text:
        break

Could someone help me? Thanks a lot.


Solution

  • You're trying to get list of elements (driver.find_elements_by_class_name() might return list of elements or empty list - no exceptions) - you cannot get NoSuchElementException in this case, so driver.refresh will not be executed. Try below instead

    while True:
        if any(["Puffy" in h1.text.replace('\uFEFF', "") for h1 in driver.find_elements_by_class_name("name-link")]):
            break
        else:
            driver.refresh
    driver.find_element_by_xpath("//*[contains(., 'Puffy')]").click()