如何用Selenium-Python验证页面文本存在?无可用XPath/CSS选择器
Hey there! Let's walk through getting this sorted out. The AssertionError you're hitting is because you tried to reference element.text without ever defining what element actually is in your code. No worries—let's go over a few reliable ways to check for that target text, even without a clean XPath or CSS selector.
Method 1: Check the Entire Page Source (Quick but Broad)
If you just need to confirm the text exists anywhere on the page (including hidden content in the raw HTML), you can adjust your regex approach to work correctly:
import re from selenium import webdriver driver = webdriver.Chrome() # Swap with your browser of choice driver.get("your_target_page_url") # Grab the full page source and search for the text page_source = driver.page_source text_match = re.search(r'heading of the page', page_source) # Assert that the text was found assert text_match is not None, "Text 'heading of the page' not found on the page"
Heads up: This checks the raw HTML, so it might pick up the text even if it's not visible to the user on the rendered page.
Method 2: Target the Exact Div Using Its Inline Style
Your target div has a unique inline style that we can leverage to select it directly. This is the most precise approach since it targets the exact element you identified:
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("your_target_page_url") # Use a CSS selector that matches the div's style attributes target_div = driver.find_element(By.CSS_SELECTOR, "div[style*='width:161.75mm'][style*='min-width: 161.75mm']") # Assert the text matches exactly (strip removes any accidental whitespace) assert target_div.text.strip() == 'heading of the page', "Div text does not match expected value"
Why this works: The *= operator in CSS selectors checks if the attribute contains the specified substring. This ensures we're grabbing the exact div you inspected, not just any random element with the text.
Method 3: Find Any Visible Element Containing the Text
If you don't care about the specific element and just want to confirm the text is visible on the page, use an XPath to locate elements with that text:
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("your_target_page_url") # Find all elements that contain the target text (substring match) matching_elements = driver.find_elements(By.XPATH, "//*[contains(text(), 'heading of the page')]") # Assert at least one element was found assert len(matching_elements) > 0, "No element found with text 'heading of the page'" # Optional: If you need an exact text match (case-sensitive, no extra spaces) exact_match_elements = driver.find_elements(By.XPATH, "//*[text()='heading of the page']") assert len(exact_match_elements) > 0, "No element found with exact text 'heading of the page'"
Key Fixes for Your Original Code
Your initial code had two clear issues:
- You never defined
elementbefore trying to accesselement.text—that's the direct cause of your AssertionError. - If you wanted to stick with the regex approach, you need to assert that
text_foundis notNone, not compare it to an undefined element's text.
Pick the method that fits your needs best—Method 2 is probably the most reliable since it targets the exact div you identified in the inspector.
内容的提问来源于stack exchange,提问作者PythonCoder4_09

