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

Selenium Java如何点击仅带aria-label标识的role=radio星级评分div元素

问题核心原因

  1. 固定iframe索引的切换方式不稳定,页面加载时iframe的加载顺序可能发生变化,导致实际切入的不是评论弹窗的iframe
  2. 你定位的带aria-label的span元素本身没有绑定点击事件,实际可交互的是它的父级div元素
  3. 谷歌地图的自定义UI组件,Selenium原生点击经常无法触发前端绑定的交互事件

调整后代码

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class GoogleMapsReviewTest {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        // 全局隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

        try {
            // 打开目标商户页面
            driver.get("https://www.google.com/maps/place/Plaza+San+Miguel/@-12.0767727,-77.0852847,17z/data=!3m1!4b1!4m5!3m4!1s0x9105c970336b0725:0xae2a6f969b48786e!8m2!3d-12.076942!4d-77.0831114?shorturl=1");

            // 等待撰写评论按钮可点击后点击
            WebElement writeReviewBtn = wait.until(ExpectedConditions.elementToBeClickable(
                    By.xpath("//button[@data-value=\"Escribir una opinión\"]")
            ));
            writeReviewBtn.click();

            // 等待评论弹窗iframe加载完成,不使用固定索引,按特征匹配iframe后切换
            WebElement reviewFrame = wait.until(ExpectedConditions.presenceOfElementLocated(
                    By.xpath("//iframe[contains(@src, 'googleusercontent.com')]")
            ));
            driver.switchTo().frame(reviewFrame);

            // 输入评论内容
            WebElement reviewInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea")));
            reviewInput.sendKeys("This is just a test");

            // 定位四星对应的父级可点击元素
            WebElement fourStar = wait.until(ExpectedConditions.visibilityOfElementLocated(
                    By.xpath("//div[.//span[@aria-label=\"Cuatro estrellas\"]]")
            ));

            // 用JS执行点击,避开Selenium原生点击的限制
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].click();", fourStar);

            // 等待发布按钮可点击后点击
            WebElement publishBtn = wait.until(ExpectedConditions.elementToBeClickable(
                    By.xpath("//button[@aria-label=\"Publicar\"]")
            ));
            publishBtn.click();

        } finally {
            // 操作完成后停留3秒关闭驱动
            Thread.sleep(3000);
            driver.quit();
        }
    }
}

额外说明

  • 所有等待都换成显式等待,比固定Thread.sleep兼容性更强,不会因为网络波动导致元素未加载完成就执行操作
  • 如果还是点击失败,可以在点击星级前加一步hover操作,模拟手动选择星级前的悬停动作,适配谷歌地图的星级交互逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 10:06:03