如何使用Nightwatch定位子Frame中的元素?WebTour场景问题求助
Got it, let's break down why your .frame(0).frame(1) approach isn't working and walk through actionable fixes to target that login input in the WebTour scenario.
First, let's recap the HTML structure you shared for clarity:
Here are the key solutions to try:
Use the frame's
nameattribute directly (most reliable)
Since the target frame has an explicitname="b"attribute, skip indexing entirely and target it by name. This avoids headaches if the page structure ever changes (indexes can shift, but names are usually static). For example, in Selenium (the standard tool for WebTour exercises), you'd use:driver.switch_to.frame("b") # Switch straight to the frame named "b" login_input = driver.find_element(By.NAME, "login")If you're using a different automation tool, look for methods that accept frame names instead of numeric indexes.
Fix your frame indexing logic
Your.frame(0).frame(1)suggests you're trying to chain nested frame switches—but in your HTML, the frames are siblings under a single<frameset>; there's no nested frame structure here. You only need to switch to one frame directly. If you still want to use indexes, target the second frame (index 1, since indexes start at 0) in the top-level frameset:driver.switch_to.frame(1) # Switch to the second frame in the framesetChaining frame switches would only be necessary if frame 0 contained another frame with index 1, which isn't the case here. That's why your original approach failed.
Wait for the frame to load before switching
Sometimes frame content loads asynchronously, so trying to switch immediately might result in a "frame not found" error. Add an explicit wait to ensure the frame is fully loaded before interacting with it. In Selenium, that would look like:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait up to 10 seconds for the frame to be available and switch to it WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "b"))) login_input = driver.find_element(By.NAME, "login")Reset to default content first
If you've previously switched to another frame during your test, you need to switch back to the top-level page context before targeting the frameset's frames. Use this line to reset:driver.switch_to.default_content() # Go back to the main page context driver.switch_to.frame("b") # Now switch to the target frame
Start with the first solution (using the frame name)—it's the simplest and most reliable way to avoid index-related issues.
内容的提问来源于stack exchange,提问作者leon shane

