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

使用Selenium SafariDriver时遇AbstractMethodError异常求助

关于Selenium SafariDriver启动失败的问题

作为Selenium SafariDriver新手,参考示例代码编写了Test_SafariDemo测试类,预期执行后启动Safari浏览器实例、打开谷歌网站并执行搜索,但实际抛出异常,无法实现预期效果。

测试代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.remote.service.DriverService.*;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


// import org.openqa.selenium.Saf
// I have not imported the following
// import org.openqa.selenium.safari.SafariDriver;
// because this does not provide me with the SafariDriver.
public class Test_SafariDemo
{
    // the purpose of this class is to test the Safari driver
    // Written by Michael John Little
    // Based on a Java program from...
    // https://www.browserstack.com/guide/run-selenium-tests-on-safari-using-safaridriver

    public static void main(String[] args)
    {
        //create an instance of the Safari class
        WebDriver drvr =new SafariDriver();

        // lets Launch the Google website
        drvr.navigate().to("http://www.google.com/");

        // lets click on the search box and send a value
        drvr.findElement(By.id("lst-ib")).sendKeys("BrowserStack");

        // click the search button
        drvr.findElement(By.name("btnK")).click();

        // lets wait and display before we close the browser
        drvr.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        //close the browser
        drvr.close();

    }// close public static void main(String[] args)


} // public class Test_SafariDemo

抛出的异常信息

Exception in thread "main" java.lang.AbstractMethodError: 
Receiver class org.openqa.selenium.safari.SafariDriverService$Builder does not define or inherit 
an implementation of the resolved method 'abstract com.google.common.collect.ImmutableList createArgs()' 
of abstract class org.openqa.selenium.remote.service.DriverService$Builder.
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:358)
    at org.openqa.selenium.safari.SafariDriverService.createDefaultService(SafariDriverService.java:75)
    at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:60)
    at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:49)
    at Test_SafariDemo.main(Test_SafariDemo.java:27)

Process finished with exit code 1

已完成的操作

  • 在Safari开发者菜单中勾选“Allow Remote Automation”选项
  • 执行了safaridriver --enable命令

解决建议

  • 检查依赖版本兼容性:这个AbstractMethodError通常是Selenium与Guava(谷歌工具库)版本不匹配导致的。比如Selenium 4.x需要对应版本的Guava,需确保项目中依赖的Guava版本与当前Selenium版本兼容,避免依赖冲突。若使用Maven/Gradle,直接更新到匹配版本即可。
  • 清理无效导入:代码中import org.openqa.selenium.remote.service.DriverService.*;属于无效导入,可能引发类加载冲突,建议删除所有未使用的导入,只保留必要的类导入。
  • 确认浏览器与驱动版本匹配:SafariDriver与Safari浏览器版本绑定,确保你的Safari浏览器是最新稳定版,或处于当前Selenium版本支持的范围内。
  • 显式配置驱动服务(可选):若上述方法无效,可尝试显式构建SafariDriverService初始化驱动,示例代码如下:
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import java.io.File;

// ...
SafariDriverService service = new SafariDriverService.Builder()
    .usingDriverExecutable(new File("/usr/bin/safaridriver"))
    .usingAnyFreePort()
    .build();
WebDriver drvr = new SafariDriver(service);
// ...

内容的提问来源于stack exchange,提问作者Flash Jack From Gundagai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 23:27:45