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

如何使用Selenium滑动滑块至指定客户引用内容?

解决方案:滑动滑块到指定客户引用内容

Hey there! Let's sort out that slider scrolling to get your target client quotes (Eversave, Simplymap, and Pattern Publishing) visible. Here's how to update your Selenium code:

完整修改后的代码

package test;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.gecko.driver", "C:/Users/goran/Desktop/Alas/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        
        // 搜索并打开wedoqa.com
        driver.get("http://www.google.com");
        WebElement search = driver.findElement(By.name("q"));
        search.sendKeys("wedoqa.com\n");
        search.submit();
        Thread.sleep(5000);
        
        WebElement firstResult = driver.findElement(By.xpath("//h3[@class='r']/a"));
        firstResult.click();
        Thread.sleep(1000);
        
        // 滚动到testimonials区域
        WebElement testimonialsSection = driver.findElement(By.id("testimonials"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", testimonialsSection);
        Thread.sleep(2000);
        
        // --- 新增:横向滚动到目标客户引用 ---
        // 滚动到Eversave的引用
        WebElement eversaveQuote = driver.findElement(By.xpath("//div[contains(text(), 'Eversave')]/ancestor::div[contains(@class, 'testimonial-item')]"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'nearest', inline: 'start'});", eversaveQuote);
        Thread.sleep(1000);
        
        // 滚动到Simplymap的引用
        WebElement simplymapQuote = driver.findElement(By.xpath("//div[contains(text(), 'Simplymap')]/ancestor::div[contains(@class, 'testimonial-item')]"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'nearest', inline: 'start'});", simplymapQuote);
        Thread.sleep(1000);
        
        // 滚动到Pattern Publishing的引用
        WebElement patternQuote = driver.findElement(By.xpath("//div[contains(text(), 'Pattern publishing')]/ancestor::div[contains(@class, 'testimonial-item')]"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'nearest', inline: 'start'});", patternQuote);
        Thread.sleep(1000);
        
        driver.quit();
    }
}

关键说明

  • scrollIntoView({block: 'nearest', inline: 'start'}): 这个配置专门针对横向滚动容器,确保目标元素在横向视图的起始位置完整显示,避免只滚动到部分区域。
  • XPath定位: 通过客户名称定位对应的引用元素,再滚动到它,比硬编码滚动距离更可靠——不会因为页面布局调整而失效。
  • 等待时间: 添加Thread.sleep()是为了让滚动动画完成,实际项目中建议用WebDriverWait替代硬等待,稳定性会更高。

内容的提问来源于stack exchange,提问作者Neo Cortex

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:03:34