如何使用Selenium和Python点击网页中的Continue Checkout GIF元素?
Hey there, let's work through why you're having trouble clicking that Continue Checkout GIF button. Looking at your code and the HTML snippet you shared, here are targeted fixes to try:
1. Use the Element's Unique ID (Most Reliable)
The button has a unique id="formSubmit" attribute—this is the most stable way to locate it, way better than relying on the dynamic src value which might change. Replace your last line of code with this:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "formSubmit"))).click()
2. Try a More Precise CSS Selector
If the ID approach doesn't work for some reason, use a CSS selector that combines multiple attributes of the button to narrow it down:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.gaTrackedEvent[type='image'][value='Continue Checkout']"))).click()
3. Check for Overlays or Use Action Chains
Sometimes elements are hidden behind overlays, or the browser needs a little nudge to recognize the click. Try using ActionChains to move to the element first before clicking:
from selenium.webdriver.common.action_chains import ActionChains button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "formSubmit"))) action = ActionChains(driver) action.move_to_element(button).click().perform()
4. Clean Up Wait Conflicts
Quick side note: You're mixing explicit waits (WebDriverWait) and implicit waits (driver.implicitly_wait) in your code. This can cause unexpected timing issues. Stick to explicit waits throughout your script for consistency.
Also, your "Out of Stock" check has an invalid XPath—you probably meant something like:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Out of Stock!']")))
Give these fixes a shot, starting with the ID-based locator—it should resolve the click issue.
内容的提问来源于stack exchange,提问作者Stephen Brown

