Appium无法定位视图内UIButton问题咨询
Hey there, let's work through this tricky Appium issue you're facing. That button showing enabled=true but visible=false is definitely throwing a wrench in things—here are some practical fixes to try:
1. Fix Your Wait Condition First
The biggest issue in your current code is that you're waiting for visibility of the button, but Appium's visibilityOfAllElements will never pass because the button's visible property is false. Instead, switch to waiting for the element to exist (since it's already enabled and accessible):
public void selectRatingWithStar(int numOfClicks, int position){ System.out.println("before driver"); // Replace visibility check with presence check—we don't need it to be visible new WebDriverWait(driver,10).until(ExpectedConditions.refreshed( ExpectedConditions.presenceOfAllElementsLocatedBy(By.accessibilityId("your-button-access-id")) )); System.out.println("after driver"); List<WebElement> starButtons = driver.findElements(By.accessibilityId("your-button-access-id")); for(int i = 0; i < numOfClicks; i++){ starButtons.get(position).click(); } }
This skips the visibility check entirely and waits for the element to be present in the DOM, which it should be since Appium Inspector can see it.
2. Verify the Button's Actual Layout
Even if you don't see nested views, double-check:
- In Appium Inspector, compare the button's
framewith its parent StackView/TableViewCell'sframe. If the button's frame is outside the parent's bounds, iOS will mark it as invisible even if it's technically there. - Use Xcode's View Debugger (Debug > View Debugging > Capture View Hierarchy) to inspect the layout—sometimes transparent overlays or incorrect constraints can make elements visually hidden.
3. Adjust Appium Capabilities to Ignore Visibility Filters
Add this desired capability to your Appium setup to allow locating elements even if they're not visible:
capabilities.setCapability("allowInvisibleElements", true);
This tells Appium not to filter out elements with visible=false during locating, which should let you grab the button via its accessibility ID directly.
4. Fallback: Coordinate-Based Click (Temporary Fix)
If all else fails, you can calculate the button's position relative to its parent cell/stack view and click via coordinates:
WebElement parentCell = driver.findElement(By.xpath("//your-table-view-cell-xpath")); Point cellLocation = parentCell.getLocation(); Dimension cellSize = parentCell.getSize(); // Calculate button's relative position (adjust x/y based on your layout) int buttonX = cellLocation.getX() + (cellSize.getWidth() / 2); int buttonY = cellLocation.getY() + (cellSize.getHeight() / 2); new TouchAction(driver).tap(PointOption.point(buttonX, buttonY)).perform();
Note: This is a temporary workaround—if your app's layout changes, this will break, so prioritize the earlier fixes.
Quick Check: Ensure isUserInteractionEnabled is True
In iOS, even if enabled=true, if isUserInteractionEnabled is false on the button or its parent, you won't be able to click it. Confirm this property is set to true in your app's code for the button.
内容的提问来源于stack exchange,提问作者Mike Koofer

