Home > Blockchain >  Scrapping with python and selenium, looking for the good loops
Scrapping with python and selenium, looking for the good loops

Time:12-16

I wonder how to make a loop that looks for several scenarios and acts differently depending on which one it finds, either scenario 1 and performs certain actions or scenario 2 and performs other actions.

Let me explain, I need the loop to do something like this:

1/ click on the drop-down menu. drop-down menu is : btnOptions = WebDriverWait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div...."))).click()

2/ searches for the presence of the elements (1 and 2).

3/ if element 1 found, do some actions and restart the loop at the beginning. first element : (By.XPATH, "//*[text()='My text1']")

4/ if element 2 found, do some actions and restart the loop at the beginning. second element : (By.XPATH, "//*[text()='My text2']")

i tried several codes but each time the program stop since he finds one element and give an error because it do not find the other element. first one was this one :

while True:
try:
    btnOptions = WebDriverWait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div...."))).click()
    btnTrash = WebDriverWait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='My text1']"))).click()
    btn_move = WebDriverWait(driver, 1000).until(EC.presence_of_element_located((By.XPATH,
                                                                               "/html/body/...."))).click()
    time.sleep(2)
except NoSuchElementException:
    break

this is my first code, it works for the first scenario, at times a new scenario appears, this one becomes obsolete and generates an error then the program stops

i tried this :

while True:
try:
    btnOptions = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/..."))).click()
    firstElement = WebDriverWait(driver, 10).until(driver.find_elements(By.XPATH, "//*[text()='My text1']"))
    if not firstElement:
        btnTrash = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='My text2']"))).click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
                                                                    "/html/body/...."))).click()
    else:
        firstElement.click()
except NoSuchElementException:
    break

this does not work.

I tried this also :

while True:
try:
    btnOptions = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/...."))).click()
    WebDriverWait(driver, 20).until(driver.find_element(By.XPATH("//*[text()='My text1']"))).click() or WebDriverWait(driver, 20).until(driver.find_element(By.XPATH("//*[text()='My text2']"))).click()
except NoSuchElementException:
    break

there must be something simple to do but I can't, no matter what I try it doesn't work

CodePudding user response:

You can use another try except statement within the other one:

try:
   #some action
except:
   try:
      #some other action
   except:
      #what to do if the two previous ones failed
  • Related