fightstarr20
fightstarr20

Reputation: 12618

Python Selenium check html element is present

I am using python selenium with firefox to check if a pages title

WebDriverWait(driver, 60).until(lambda x: 'Page 1' in driver.title or 'Page 2' in driver.title)

I am now wanting to also check if a HTML element is present, can I do this within this WebDriverWait statement or should I further process the result?

Upvotes: 1

Views: 115

Answers (1)

alecxe
alecxe

Reputation: 474171

One option would be to reuse the same WebDriverWait() instance and issue a follow-up wait:

wait = WebDriverWait(driver, 60)
wait.until(lambda x: 'Page 1' in driver.title or 'Page 2' in driver.title)
wait.until(EC.presence_of_element_located((By.ID, "myid")))

Or, you can also combine them in a single function:

def mywait(driver):
    if 'Page 1' in driver.title or 'Page 2' in driver.title:
        return EC.presence_of_element_located((By.ID, "myid"))(driver)

wait = WebDriverWait(driver, 60)
wait.until(mywait)

Upvotes: 2

Related Questions