You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python3 Selenium直接跳转到指定页码(以第5页为例)?

Hey there! Since you already know the exact __doPostBack function that handles pagination on this ASP.NET page, executing it directly via Selenium's JavaScript executor is the perfect way to jump straight to page 5 without clicking through each page manually. Here's how to implement it:

Directly Jump to Page 5 Using Selenium & __doPostBack

Step-by-Step Code Implementation

  1. Load the Initial Search Page
    First, navigate to the target page and wait for critical elements to load (this prevents errors from trying to run scripts on an incomplete page):

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    # Initialize your browser driver (adjust for Chrome/Firefox/Edge as needed)
    driver = webdriver.Chrome()
    target_url = "https://dciindia.gov.in/DentistsSearch.aspx?Reg_Type=D&RegUnder=0&IDRId=&IDRName=&CourseId=0&RegDate=0&CouncilId="
    driver.get(target_url)
    
    # Wait for the dentist list grid to be present (ensures page is ready)
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder3_gvSearchDentistlist"))
    )
    
  2. Execute the Postback Script
    Use Selenium's execute_script method to run the exact __doPostBack command you identified:

    # Trigger the jump to page 5 directly
    driver.execute_script("__doPostBack('ctl00$ContentPlaceHolder3$gvSearchDentistlist','Page$5');")
    
  3. Verify Page 5 Loaded
    Add a wait to confirm the page has updated to page 5 (adjust the selector based on how the page indicates active pagination):

    # Example: Wait for the "5" pagination link to be marked as active
    WebDriverWait(driver, 10).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, ".pagination .active"), "5")
    )
    

Key Notes

  • Why This Works: __doPostBack is an ASP.NET-specific function that sends a server request with pagination parameters. By running it directly, you skip the manual page clicks entirely.
  • Parameter Accuracy: Double-check that the element ID (ctl00$ContentPlaceHolder3$gvSearchDentistlist) and page string (Page$5) are exactly as they appear in the page's source—these are case-sensitive and must match perfectly.
  • Error Handling: If the script doesn't work, ensure the grid element is fully loaded before executing the postback, and check for any dynamic changes to the element IDs on the page.

内容的提问来源于stack exchange,提问作者Aditya Anand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:22:28