Selenium新手求助:如何定位并点击指定HTML图片元素?
Hey there! Let's work through this problem step by step—you're close, just a few small missteps are blocking you from getting that zoom functionality working.
First, Let's Diagnose Why Your Previous Attempts Failed
Let's break down what went wrong with each approach:
document.body.style.zoom='0.4'not working: The page's stream image has fixedwidth="2560"andheight="1920"attributes, which likely override the body zoom style. Plus, some pages use container-specific scaling instead of body-level zoom, so this method often doesn't play nice with embedded media like your stream view.- Incorrect XPath for the zoom button: You used
../pics/button_downsize_27x27px.gifin your XPath, but the actualsrcin the HTML is/pics/button_downsize_27x27px.gif(no../prefix). That mismatch meant Selenium couldn't find the element at all. - Using
find_elements(plural) incorrectly: Methods likedriver.find_elements_by_id()ordriver.find_elements_by_tag_name()return a list of elements, not a single element. You can't call.click()directly on a list—you'd need to loop through it or pick the correct index, which isn't ideal here. - Invalid XPath syntax:
//*[@img]isn't a valid query—@imgdoesn't refer to a real attribute. You meant to target<img>elements directly, but even then, that would select all images (including the stream view), not just the zoom button.
Working Solutions to Click the Zoom Button
Here are a few reliable ways to locate and click that downsize button:
1. Use the Exact (Correct) src Attribute in XPath
Fix the path mismatch in your original XPath, and use find_element (singular) to target the specific image:
driver.find_element_by_xpath('//img[@src="/pics/button_downsize_27x27px.gif"]').click()
2. Use the title Attribute for More Reliability
The button has a descriptive title attribute ("Scale down to 800 px width") which is less likely to change than the image path. This makes your locator more robust:
driver.find_element_by_xpath('//img[@title="Scale down to 800 px width"]').click()
3. Target the Parent <a> Tag (Linked to the Zoom Function)
Since the button is wrapped in an <a> tag that calls JavaScript:zoom(8), you can target that link directly, then click its child image:
driver.find_element_by_xpath('//a[contains(@href, "JavaScript:zoom(8)")]/img').click()
4. Skip Locating the Element—Run the JavaScript Directly
Instead of clicking the button, you can execute the same zoom function that the button triggers. This is often faster and avoids element-location issues entirely:
driver.execute_script("zoom(8);")
Add Explicit Waits for Better Stability
Even with implicitly_wait, sometimes elements take a moment to become clickable. Using explicit waits ensures Selenium doesn't try to click the button before it's ready:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Wait up to 10 seconds for the button to be clickable zoom_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//img[@title="Scale down to 800 px width"]')) ) zoom_button.click()
Bonus: Adjusting the Stream Image's Size Directly
If clicking the button or running the JS doesn't work, you can modify the stream image's width and height attributes directly:
stream_img = driver.find_element_by_id("stream") driver.execute_script("arguments[0].setAttribute('width', '800')", stream_img) driver.execute_script("arguments[0].setAttribute('height', '600')", stream_img)
内容的提问来源于stack exchange,提问作者Tzahi Kadosh

