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

Selenium加载时间错误排查及可行解决方案咨询

Hey there! Let's break down each of your issues and walk through practical fixes, since they all relate to handling dynamically loaded content with Selenium.

Issue 1: Incorrect Last Page Number (Shows 1 instead of 197)

This happens because you're grabbing the element's text before the page finishes loading the actual pagination data via JavaScript. The site probably renders a placeholder "1" first, then updates it once the AJAX call completes.

Fix:

Use explicit waits to ensure the element is visible, and add a check to wait for the text to update from the initial placeholder:

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Wait for the pagination element to be visible
last_page_element = WebDriverWait(driver, 15).until(
    EC.visibility_of_element_located((By.CLASS_NAME, 'right-center'))
)

# Wait until the text isn't just the initial "1" (adjust if the placeholder is different)
while last_page_element.text.strip() == "1":
    time.sleep(0.5)
    # Refresh the element reference to get updated text
    last_page_element = driver.find_element_by_class_name("right-center")

last_page = int(last_page_element.text)
print(f"Loaded actual last page: {last_page}")

Why this works:

The explicit wait ensures the element exists and is visible, while the loop waits for the JavaScript to finish updating the pagination value. We refresh the element reference each time to make sure we're getting the latest text.


Issue 2: NoSuchElementException for the 'test' Element

Your XPath has a syntax error that's causing the failure. The part [@class='col-lg-3 col-sm-3 result-info' and 2] is invalid—you can't combine a class attribute check with a numeric index like that. You need to wrap the element selection in parentheses and then apply the index.

Fix:

Correct the XPath and use explicit waits to ensure the element is visible:

# Corrected XPath: selects the 2nd div with class 'result-info', then its first 'brand-name' span
test_element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, "(//div[@class='col-lg-3 col-sm-3 result-info'])[2]/span[@class='brand-name']"))
)
print(test_element.text)

Additional Checks:

  • If this still fails, verify the element isn't inside an <iframe>—if it is, you'll need to switch to the iframe first with driver.switch_to.frame(iframe_element).
  • Avoid overly specific XPaths; using CSS selectors is often more stable: (div.result-info:nth-child(2)) span.brand-name (adjust the index if needed).

Issue 3: ElementNotVisibleException When Clicking Last Page

This usually means the element exists in the DOM but isn't visible on the page (e.g., it's off-screen, hidden by another element, or hasn't finished rendering).

Fix:

Wait for the element to be clickable, scroll it into view, and use JavaScript click if necessary:

last_page_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CLASS_NAME, 'right-center'))
)

# Scroll the element into view to ensure it's visible
driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", last_page_button)

# Try regular click first
try:
    last_page_button.click()
except:
    # Fallback to JavaScript click if regular click fails (bypasses visibility checks)
    driver.execute_script("arguments[0].click();", last_page_button)

Why this works:

  • element_to_be_clickable waits until the element is both visible and enabled.
  • Scrolling into view ensures the browser can interact with it.
  • JavaScript click is a last resort for elements that are technically present but blocked by UI overlays.

General Best Practices
  • Avoid absolute XPaths: Your login code uses a long absolute path (/html/body/div[@class='wrapper']/...), which breaks easily if the page structure changes. Replace it with relative paths like //a[@class='login-link'] or //input[@id='email'].
  • Prefer explicit waits over time.sleep: They're more efficient and reliable since they wait only as long as needed.
  • Use stable locators: IDs, unique class names, or attribute-based selectors are better than fragile XPaths that depend on nested structure.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:18:32