Python Forum
Inconsistent loop iteration behavior
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistent loop iteration behavior
#1
I have some code that opens a website, submits search criteria, and loops through each resulting table row for the link to the details behind it. The results may be broken into pages so it keeps track of that too. It works largely fine with the exception that occasionally, and usually on the first row of a newly loaded page, it will just sit there and not follow the link. I can see the link indicate that it was hovered over, but nothing happens, until I click on it with the mouse, then the process continues. I just don't know what I am missing, as it is placed within a loop that should retry the click as long as the right page is there, which is identified by the presence of known elements. The while loop that starts at line 29 is the functional part in question, and line 31 is where the link is clicked. Code below:

def pagescrape(driver,page_no):
    global page_count, resume_record_number
    results_table_details_link_xpath="//table[@class='results']/tbody/tr/td[1]/a[contains(@href,'inquiryDetail')]"
    captcha_page_element_xpath="//*[@class='captcha__human__title' and text()='You have been blocked.']"
    try:
        wait=WebDriverWait(driver,20)
        wait.until(EC.element_to_be_clickable((By.XPATH,results_table_details_link_xpath)))
        try:                                                                  
            result_banner=driver.find_element(By.CLASS_NAME,"pagebanner").text                                                      #Search Results page
            result_banner_list=result_banner.split()
            search_total=int(result_banner_list[0])
            if search_total<=25:
                min_rec=1
                max_rec=search_total
            else:
                min_rec = int(result_banner_list[4])
                max_rec = int(result_banner_list[6].replace('.', ''))
            record_count=max_rec-min_rec+1
            page_header_tag=str(str(page_no)+'/'+str(page_count)).ljust(10,' ')
            print(f"Page:{page_header_tag} Search Total:{str(search_total).ljust(6,' ')} Search Letter:{letter_current}  Profile:{profile_current}")
            wait=WebDriverWait(driver,6)
            for i in range(resume_record_number,record_count+1):
                row_tag=f"{i}/{record_count}".ljust(10,' ')
                case_status=driver.find_element("xpath", f"//table[@class='results']/tbody/tr[{i}]/td[7]").text
                if case_status=='Open' or case_status=='ACTIVE CASE':
                    Case_Number=driver.find_element("xpath", f"/html/body/div/table[4]/tbody/tr[{i}]/td[1]/a").text
                    case_type=driver.find_element("xpath", f"/html/body/div/table[4]/tbody/tr[{i}]/td[6]").text
                    time.sleep(1)
                    while True:
                        try:
                            details_link=driver.find_element("xpath", f"/html/body/div/table[4]/tbody/tr[{i}]/td[1]/a")
                            driver.execute_script("return arguments[0].scrollIntoView();", details_link)
                            details_link.click()
                            wait.until(EC.any_of 
                                (EC.text_to_be_present_in_element((By.CLASS_NAME,'InfoStatement'),'This is an electronic case record'),
                                EC.element_to_be_clickable((By.XPATH,"//div[@class='Subheader']/a[text()='Go Back']"))
                                ))
                            html=driver.find_element(By.TAG_NAME, "html")                                                       #Details page
                            for j in range(randrange(5)):
                                html.send_keys(Keys.PAGE_DOWN)
                                driver.find_element("xpath",f"/html/body").click();
                            for j in range(randrange(5)):
                                html.send_keys(Keys.PAGE_UP)
                                driver.find_element("xpath",f"/html/body").click();
                            time.sleep(1)
                            details=getrecord(driver,Case_Number,page_no,case_type)
                            values_list=list(details.values())
                            print(f"{row_tag}  {values_list[8]}")
                            with open(proc_path, 'a',encoding="utf-8",newline="") as f:
                                writer = csv.writer(f)
                                writer.writerow(values_list)
                            driver.back()
                            time.sleep(1)
                            break
                        except TimeoutException as timeout_results_page:
                            try:
                                wait.until(
                                    EC.any_of(
                                        EC.element_to_be_clickable((By.XPATH,results_table_details_link_xpath)),
                                        EC.text_to_be_present_in_element((By.CLASS_NAME,'captcha__human__title'),'You have been blocked.'),
                                        EC.text_to_be_present_in_element_value((By.NAME,'button'),'Go Back'),
                                        EC.element_to_be_clickable((By.ID,"btnDisclaimerAgree")),
                                    )
                                )
                                if len(driver.find_elements(By.XPATH,results_table_details_link_xpath))>0:      #Search results page
                                    time.time(1)
                                    print(f'Retrying record: {i}')
                                    continue
                                elif len(driver.find_elements(By.XPATH,captcha_page_element_xpath))>0:          #captcha page
                                    continue
                                elif len(driver.find_elements(By.NAME,"button"))>0:                             #Error page. Title: An Error has occurred
                                    print(f"{row_tag}  --  Error page loaded")
                                    backbutton=driver.find_element(By.NAME,"button")
                                    backbutton.click()
                                    time.sleep(2)
                                    break
                                else:                                                                           #Back at home/disclaimer page
                                    resume_record_number=i
                                    return i
                            except Exception as err_long_timeout:
                                if 'Search Results' in driver.title:
                                    continue
                                else:
                                    print(f"Page Title: {driver.title} - {err_long_timeout}  --  in-search page wait")
                                    continue
                        except Exception as err_other:
                            print(err_other)
                            continue
                    while True:
                        try:
                            wait.until(EC.element_to_be_clickable((By.XPATH,results_table_details_link_xpath)))
                            break
                        except Exception as err_results:
                            print(f"Page Title: {driver.title} - {err_results}  --  Results page expected")             
                else:
                    print(f"{row_tag}  --  Closed Case")
                    continue
            resume_record_number=1
            return -1
        except Exception as err_results_page:
            return -3
    except Exception as e:
        print(f"{e}  --  Catch-all")
        return -3
Any insight would be greatly appreciated
Reply
#2
you should consider breaking your code into functions, code is very difficult to follow as is.
ndc85430 likes this post
Reply
#3
(Dec-10-2022, 03:52 AM)Larz60+ Wrote: you should consider breaking your code into functions, code is very difficult to follow as is.

Ok. Thank you for that advice
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read csv file with inconsistent delimiter gracenz 2 1,207 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,482 Jun-16-2022, 06:59 PM
Last Post: saoko
  Loop Dict with inconsistent Keys Personne 1 1,613 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Inconsistent counting / timing with threading rantwhy 1 1,774 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Inconsistent behaviour in output - web scraping Steve 6 2,564 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,910 Sep-15-2021, 12:48 PM
Last Post: jenya56
  Packages inconsistent warning during hdbscan install Led_Zeppelin 0 1,933 Aug-31-2021, 04:10 PM
Last Post: Led_Zeppelin
  saving each iteration of a loop sgcgrif3 3 6,742 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  String slicing and loop iteration divyansh 9 4,760 Jun-07-2020, 10:29 PM
Last Post: divyansh
  inconsistent map() behavior mcmxl22 2 2,243 May-23-2020, 10:17 PM
Last Post: mcmxl22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020