求助:如何使用Selenium爬取Messenger并查看仅鼠标悬停时显示的元素(无需悬停优先)
Hey there! Let's figure out how to access that hover-only element on Messenger without manually hovering first. We'll start with the most efficient approaches (no hover required) before falling back to simulating a hover if needed.
Most hover-triggered elements are hidden via CSS—think display: none, opacity: 0, or a dedicated hidden class. Instead of triggering a hover, we can directly modify the element's styles to make it visible using Selenium's JavaScript execution feature.
Here's how to do it in Python:
- First, grab the element's unique selector from your browser's DevTools (right-click the element > Copy > Copy selector).
- Run this script to override the hidden styles:
# Replace these with your actual selectors target_selector = "your-hover-element-selector" driver.execute_script(f""" const element = document.querySelector('{target_selector}'); if (element) {{ // Unhide the element by resetting key CSS properties element.style.display = 'block'; // Adjust to 'inline' or 'flex' if needed element.style.opacity = '1'; element.style.visibility = 'visible'; // If a specific class hides it, remove that class element.classList.remove('hidden'); // Replace with the actual hidden class name }} """)
This works because we're bypassing the :hover pseudo-class entirely—we're making the element visible directly in the DOM.
Sometimes you don't even need to make the element visible to get its content. If the text or data is already present in the DOM (just hidden from view), you can extract it with JavaScript:
element_content = driver.execute_script(f"return document.querySelector('{target_selector}').textContent;") print(element_content)
This is super useful if you only need the text/data and don't need to interact with the element visually.
If the above methods don't work (maybe the element is only added to the DOM when hovered), then we can use Selenium's ActionChains to simulate a mouse hover on the parent element that triggers the element to appear.
Python example:
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait for the parent element to load (critical for dynamic pages like Messenger) wait = WebDriverWait(driver, 10) parent_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "parent-element-selector"))) # Perform the hover action actions = ActionChains(driver) actions.move_to_element(parent_element).perform() # Now locate and interact with the visible element target_element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, target_selector)))
Quick Tips for Messenger Specifics
- Messenger uses dynamic class names, so avoid relying on class names alone. Use stable attributes like
data-testidor combine tag names with attributes (e.g.,div[data-testid="hover-trigger"]) for more reliable selectors. - Always use
WebDriverWaitto wait for elements to load—Messenger's UI loads asynchronously, so immediate element lookups might fail.
内容的提问来源于stack exchange,提问作者user17609181

