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

Selenium两大问题:Alert弹窗处理与下拉框交互异常求助

Hey there! Let's work through these two Selenium issues you're facing—they're pretty common with enterprise web apps, so I've got some actionable fixes for you.

Problem 1: UnhandledAlertException Breaking Execution

First up, that "User already logged in another machine" popup is crashing your script because your current try/catch only covers the entire block, but the alert might be popping up inside one of your helper methods (like Login_Module or WorkOrderConsole), which means the exception isn't being caught where you expect it. Also, your code doesn't retry the workflow after handling the alert, so it just stops.

Here's a better approach that wraps the workflow in a retry loop, so it handles the alert and picks back up where it left off:

boolean workflowCompleted = false;
while (!workflowCompleted) {
    try { 
        // Login and post-login steps
        Login.Login_Module(driver, wait); 
        Login.WorkOrderConsole(driver, wait); 
        C1 = new Collector(); 
        C1.TableCollector(driver, wait);
        
        workflowCompleted = true; // Mark success if no exceptions hit
    } catch(UnhandledAlertException e) { 
        System.out.println("Caught login alert: " + e.getMessage());
        // Handle the alert immediately—no need to wait for it since the exception confirms it's open
        Alert alert = driver.switchTo().alert();
        System.out.println("Alert content: " + alert.getText());
        alert.accept();
        
        // Add a short wait to let the page reset before retrying
        Thread.sleep(1000);
    } catch (Exception generalEx) {
        // Catch other unexpected errors to avoid infinite loops
        System.err.println("Unexpected error: " + generalEx.getMessage());
        generalEx.printStackTrace();
        break; // Exit loop if it's not an alert issue
    }
}

Key fixes here:

  • The loop ensures we retry the full workflow after dismissing the alert
  • We removed the redundant wait.until(ExpectedConditions.alertIsPresent())—the exception already tells us the alert is open
  • We actually use the exception details instead of just calling e.toString() (which doesn't do anything useful on its own)

Problem 2: Custom Dropdown Not Expanding/Staying Open

Looking at your HTML, this is a custom dropdown component (not a native <select> tag), so we can't rely on Selenium's Select class. Your current Actions code has a critical bug too—you're building the click action but never executing it properly!

Let's break down the fixes:

1. Fix the Actions Click Bug

Your original code does this:

builder.moveToElement(Click_Status).click().build(); 
builder.perform(); 

The build() method returns an Action object, but you don't store it or execute it. The second line runs perform() on the empty builder, so nothing happens. Correct it to either:

// Option 1: Store and execute the action
Action clickAction = builder.moveToElement(Click_Status).click().build();
clickAction.perform();

// Option 2: Chain directly to perform()
builder.moveToElement(Click_Status).click().perform();

2. More Reliable Dropdown Handling

For custom dropdowns, we need to explicitly wait for each step (arrow click, options to load) and use stable locators (avoid dynamic IDs like WIN_3_7—use attributes like ardbn instead). Here's a robust implementation:

System.out.println("Im here"); 
Thread.sleep(1000); 

// Double-click the work order (keep this part)
Actions builder = new Actions(driver); 
WebElement Into_WO = driver.findElement(By.xpath(FXpath)); 
builder.doubleClick(Into_WO).perform(); 

// Handle the Status dropdown
// Use a stable locator for the dropdown container (based on ardbn attribute)
WebElement statusDropdownContainer = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@ardbn='Status']")));
// Scroll to the container to avoid overlap/visibility issues
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", statusDropdownContainer);

// Click the dropdown arrow (using relative locator from the container)
WebElement dropdownArrow = statusDropdownContainer.findElement(By.xpath(".//a[@class='btn btn3d selectionbtn']"));
wait.until(ExpectedConditions.elementToBeClickable(dropdownArrow)).click();

// Wait for the dropdown options to appear—you'll need to adjust this locator to match your app's rendered options
// Assuming options are in a visible menu div (check your browser's dev tools when the dropdown is open)
WebElement inProgressOption = wait.until(ExpectedConditions.elementToBeClickable(
    By.xpath("//div[contains(@class, 'selection-menu')]//*[text()='In Progress']")
));
inProgressOption.click();

System.out.println("After click"); 
// Remove System.exit(1) unless you intentionally want to terminate here

Bonus Fix for Your Loop Code

In your row iteration loop, use trim() when checking the column text—web apps often add extra whitespace that breaks string matches:

if(colElement.getText().trim().equals("Pending")) {
    // Your existing code here
}

If the dropdown still won't stay open, try using JavaScript to click the arrow instead of Actions/Selenium click:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", dropdownArrow);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:50:36