简体   繁体   中英

Repeat if until true [python]

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM