Repeat if until true [python]

Viewed 81

I want if to search for a CSS selector and if this is found, the script is carried out further, and if the CSS selector is not found, it should repeat the if function until it's true.

Here is my code:

if driver.find_elements_by_css_selector('#buyTools > div.prl6-sm.prl0-lg > fieldset > div > div:nth-child(7) > label'):
driver.find_element_by_xpath('//*[@id="buyTools"]/div[1]/fieldset/div/div[7]/label').click()
else:

Thanks for help!

2 Answers

You can use a while loop with some arbitrary wait to accomplish this like:

from time import sleep

while not driver.find_elements_by_css_selector('#buyTools > div.prl6-sm.prl0-lg > fieldset > div > div:nth-child(7) > label'):
    sleep(1)

driver.find_element_by_xpath('//*[@id="buyTools"]/div[1]/fieldset/div/div[7]/label').click()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


WebDriverWait(driver,5000).until(EC.presence_of_element_located(
    (By.CSS_SELECTOR, "#buyTools > div.prl6-sm.prl0-lg > fieldset > div > div:nth-child(7) > label")))

use in build webdriverwait for this wait to re invent the wheel using different code

Related