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

如何通过Selenium触发无链接文本的JavaScript链接?(佳能打印机场景)

Alright, let's figure out how to automate getting those daily click counts from your Canon iR5070. The tricky part here is that JavaScript-powered link you can't seem to trigger automatically—here are three solid approaches to work around this:

1. Directly Execute the Target JavaScript Function

Since the link calls load_device() when clicked, you can skip simulating a click entirely and just run that function directly in the browser context. This works great if the function doesn’t rely on any hidden page state that only gets set by manual interaction.

If you’re using Selenium for automation, here’s how you’d do it:

from selenium import webdriver

# Initialize your browser driver (Chrome in this example)
driver = webdriver.Chrome()
driver.get("http://your-printer-web-interface-url")

# First, complete all the multi-step navigation to reach the page with the link
# ... (add code for login, menu clicks, etc.)

# Execute the JavaScript function directly
driver.execute_script("load_device()")

Once you run this, the device information page should load just like it would if you clicked the link manually, and you can then scrape the daily click count from the resulting page.

2. Inspect the Actual Network Request (Most Efficient)

JavaScript functions often just wrap a backend API call or a hidden page redirect. You can uncover the actual request by using your browser’s developer tools:

  • Open the printer’s web interface and navigate to the page with the link
  • Hit F12 to open DevTools, go to the Network tab
  • Manually click the "Device Information" link
  • Look for the new request that loads the target page (check the "Name" and "Request URL" columns)

Once you have that real URL, you can bypass the web UI entirely and fetch the data directly using an HTTP client like requests in Python:

import requests

# Create a session to persist login cookies
session = requests.Session()

# First, log in to the printer's interface (adjust credentials and URL as needed)
login_payload = {
    "username": "your-printer-username",
    "password": "your-printer-password"
}
session.post("http://your-printer-ip/login-path", data=login_payload)

# Fetch the device information page directly using the URL you found
response = session.get("http://your-printer-ip/actual-device-info-path")

# Parse the response content to extract the daily click count (use BeautifulSoup if needed)
# ... (add parsing logic here)

This method is faster and more reliable than simulating a browser, since you’re interacting directly with the printer’s backend.

If executing the JS function directly doesn’t work (maybe it relies on event handlers tied to the element), you can locate the <a> tag and simulate a click on it. Again, using Selenium:

First, wait for the element to be clickable (to avoid race conditions with dynamic page loading), then click it:

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

driver = webdriver.Chrome()
driver.get("http://your-printer-web-interface-url")

# Complete preliminary navigation steps first
# ...

# Wait up to 10 seconds for the link to become clickable, then click it
wait = WebDriverWait(driver, 10)
device_link = wait.until(EC.element_to_be_clickable(
    (By.XPATH, '//a[img[@alt="Device Information"]]')
))
device_link.click()

You could also use a CSS selector to target the link:

device_link = wait.until(EC.element_to_be_clickable(
    (By.CSS_SELECTOR, 'a[href="javascript:load_device()"]')
))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:49:33