0

我有一堂课如下:

public class Utils {
static WebElement fluentWaiter(final By locator,final WebDriver driver) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(30L))
        .pollingEvery(Duration.ofSeconds(3L))
        .ignoring(org.openqa.selenium.ElementNotInteractableException.class)
        .ignoring(org.openqa.selenium.NoSuchElementException.class);

    WebElement foo = wait.until(
        new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                return driver.findElement(locator);
            }
        }
    );

    return foo;          
};   
}

当我使用 BrowserStack Safari 远程驱动程序运行代码时,我总是遇到错误:第一次尝试查找元素时出现“元素不可交互”。在控制台中,我看到以下异常:

!!! Eception: Exception: org.openqa.selenium.ElementNotInteractableException: 
!!! Exception message: Build info: version: '4.1.1', revision: 'e8fcc2cecf'
System info: host: 'DESKTOP-9Q3LLM3', ip: '192.168.0.73', os.name: 'Windows 10', 
os.arch: 'amd64', os.version: '10.0', java.version: '17.0.1'    
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [75bb87468c1fc4fa0bce6bf2bf5340d3b165c0d7, clickElement {id=node-94611454-C3E1- 
4019-82F8-76517EA0FBBB}]
Capabilities {acceptInsecureCerts: false, browserName: Safari, browserVersion: 15.1, 
javascriptEnabled: true, platform: MAC, platformName: MAC, safari:automaticInspection: 
false, safari:automaticProfiling: false, safari:diagnose: false, 
safari:platformBuildVersion: 21A559, safari:platformVersion: 12.0.1, 
safari:useSimulator: false, setWindowRect: true, strictFileInteractability: false, 
webdriver.remote.sessionid: 75bb87468c1fc4fa0bce6bf2bf5..., webkit:WebRTC: 
{DisableICECandidateFiltering: false, DisableInsecureMediaCapture: false}}
Element: [[RemoteWebDriver: Safari on MAC (75bb87468c1fc4fa0bce6bf2bf5340d3b165c0d7)] -> 
xpath: //a[@uib-tooltip='Actions'][@class='text-white']]
Session ID: 75bb87468c1fc4fa0bce6bf2bf5340d3b165c0d7

失败的代码:

       try {
          By actionsMenuListView = By.xpath("//a[@uib-tooltip='Actions'][@class='text-white']");
          Utils.fluentWaiter(actionsMenuListView, driver).click();
    } catch (Exception e) {
        System.out.println("!!! Exception: " + e.toString());
        System.out.println("!!! Exception message: " + e.getMessage());
        jse.executeScript(
                "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Download file failed\"}}");
        test.log(Status.FAIL, "Download file failed");
        assertTrue(false);
    }

为什么 FluentWait 在前 30 秒内没有忽略 ElementNotInteractableException?

同时 NoSuchElementException 按预期工作。

4

1 回答 1

0

问题出在点击操作上。将代码更改为以下代码后:

public class Utils {
  static WebElement fluentWaiter(final By locator,final WebDriver driver) {
  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30L))
    .pollingEvery(Duration.ofSeconds(3L))
    .ignoring(org.openqa.selenium.NoSuchElementException.class);

  WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    }
  );

  return foo;          
};   
}

try {
    By actionsMenuListView = By.xpath("//a[@uib-tooltip='Actions'][@class='text-white']");
    WebElement element = Utils.fluentWaiter(actionsMenuListView, driver);
    JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
    javascriptExecutor.executeScript("arguments[0].click();", element);
} catch (Exception e) {
    System.out.println("!!! Exception: " + e.toString());
    System.out.println("!!! Exception message: " + e.getMessage());
    jse.executeScript(
            "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Download file failed\"}}");
    test.log(Status.FAIL, "Download file failed");
    assertTrue(false);
}

它开始起作用了。

于 2022-02-20T09:58:44.680 回答