Thrillofit86
Thrillofit86

Reputation: 619

Selenium - Find all elements and use WebDriverWait with for loop

I have some problems with Selenium where I am trying to find all elements and then try to use WebDriverWait:

WebDriverWait(browser, 5).until(
    EC.presence_of_element_located((By.XPATH,
                                    "//*[contains(text(), 'Hello')]")))
getAllErrors = WebDriverWait(browser, 5).until(
    EC.presence_of_all_elements_located((By.CLASS_NAME, "message")))

for productErrors in getAllErrors:

    if 'Sad moments' in productErrors.text:

        totalProduct = WebDriverWait(browser, 5).until(
            EC.presence_of_element_located((productErrors.find_element_by_xpath("//input[@type='number']"))))

        #How to call productErrors and to use WebDriverWait with it?

I wonder how to use the for loop data and use WebDriverWait with the for loop varaiable?

Basically something like

for productErrors in getAllErrors:

    WebDriverWait(browser, 5).until(EC.presence_of_element_located((productErrors.find_element_by_xpath("//input[@type='number']")))) 
    #Use productErrors For loop and find the xpath from productErrors

Upvotes: 0

Views: 992

Answers (1)

Guy
Guy

Reputation: 50819

There is no presence_of for already located element, but you can use visibility_of

wait = WebDriverWait(browser, 5)
for productErrors in getAllErrors:
    wait.until(EC.visibility_of(productErrors.find_element_by_xpath("./following-sibling::div//input[@type='number']")))

Don't forget to add . to the xpath for context search. You can also declare WebDriverWait(browser, 5) one time and use it everywhere.

Upvotes: 3

Related Questions