当我的 chromedriver 文件位于正确的位置时,为什么我会收到 IllegalStateException?

问题描述 投票:0回答:1

我相信我的 chromedriver 文件在正确的位置(模糊的's ....'让我们称这个文本为“sName”):

这是我得到的错误:

我不明白的是,为什么它在我的所有其他文件中都能正常工作?例如,在左侧的 Package Explorer 中,您可以看到“section4”项目,我在其中制作了文件,这些文件让我可以转到 flipkart.com 并查看他们出售的移动电源。 “section4”项目工作得很好,我相信它也依赖于相同的 chromedriver。

flipKart.java:

package section4flipkart;

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

import utils.WaitUtility;


public class flipKart {

WebDriver driver = new ChromeDriver();
    
    String url = "https://www.flipkart.com/";
    
    public void invokeBrowser() {
        System.setProperty("webdriver.chrome.driver", "/Users/sName/eclipse-workspace/libs/chromedriver.exe");
        
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.get(url);
    }
    
    public void mouseHover() {
        driver.findElement(By.xpath("//button[@class='_2KpZ6l _2doB4z']")).click();
        
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Electronics']")));
        
        WebElement electronicsLink = driver.findElement(By.xpath("//div[text()='Electronics']"));
        
        Actions action = new Actions(driver);
        action.moveToElement(electronicsLink).build().perform();
        
        WaitUtility.waitTillElementVisible(driver, By.xpath("//a[text()='Powerbank']"), Duration.ofSeconds(10));
        WebElement powerbankLink = driver.findElement(By.xpath("//a[text()='Powerbank']"));
        action.moveToElement(powerbankLink).click().build().perform();
    }
    
    public static void main(String[] args) {
        flipKart fp = new flipKart();
        fp.invokeBrowser();
        fp.mouseHover();
    }
}

WaitUtility.java 文件:

package utils;


import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WaitUtility {
    public static void waitTillElementVisible(WebDriver driver, By by, Duration timeoutInSeconds) {
        WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
        
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    }
}

不确定为什么这不起作用。我试过打开终端,导航到 chromedriver 文件目录并运行:

spctl --add --label 'Approved' chromedriver

但不确定这是否有所作为。尝试这个时我改变了

"/Users/sName/eclipse-workspace/libs/chromedriver.exe"

"/Users/sName/eclipse-workspace/libs/chromedriver"

但我的 Mac 仍然显示“无法打开“chromedriver”,因为无法验证开发人员”弹出窗口。

java macos selenium-webdriver selenium-chromedriver
1个回答
0
投票

好的,我解决了它,感谢 chatgpt 哈哈。

这是我所做的:

  1. 我删除了文件扩展名中的“.exe”
  2. 在我收到“无法打开“chromedriver”因为无法验证开发人员”弹出窗口后,我这样做了(从 chatgpt 复制):
  • 打开 macOS 上的“系统偏好设置”。
  • 点击“安全和隐私”。
  • 在“常规”选项卡中,您应该看到一条消息说“chromedriver”被阻止使用,因为它不是来自已识别的开发人员。
  • 单击消息旁边的“仍然允许”按钮。 系统可能会提示您输入用户名和密码以授权更改。提供所需信息。
  • 再次尝试运行你的代码,它现在应该能够在没有验证错误的情况下执行 Chromedriver。
© www.soinside.com 2019 - 2024. All rights reserved.