无法用Selenium/Playwright点击Glide DataGrid单元格内的复选框
如何自动化点击Streamlit中Glide DataGrid表格内的复选框?
我需要自动化点击Streamlit开发的表格(基于Glide DataGrid组件)单元格内的复选框,先后尝试了Selenium和Playwright,但都没能实现通用且可靠的点击效果。
有没有人成功实现过Glide DataGrid组件内复选框(或其他交互元素)的自动化交互?求分享可行的技巧、替代方案或解决方法。
界面截图:
问题详情
我原本想点击st.balloons行(高亮黄色)对应的is_widget列复选框,但实际点击到了rating列显示为5的位置(高亮红色)。
尝试的代码逻辑
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.WebDriverWait; public class TestStreamlitGrid { private WebDriver driver; private WebDriverWait wait; public static void main(String[] args) { TestStreamlitGrid grid = new TestStreamlitGrid(); grid.init(); grid.click(); } private void init() { System.setProperty("webdriver.chrome.driver", "C:\\D_Drive\\chromedriver\\137\\chromedriver-win64\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("incognito"); driver = new ChromeDriver(options); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); wait = new WebDriverWait(driver, 120); try { driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("https://doc-data-editor.streamlit.app/?utm_medium=oembed&"); } catch (Exception e) { e.printStackTrace(); } } private void click() { try { JavascriptExecutor js = (JavascriptExecutor) driver; Boolean isInIFrame = !(Boolean) js.executeScript("return window.top === window.self;"); if(!isInIFrame) { WebElement frame = driver.findElement(By.xpath("//iframe[@title='streamlitApp']")); driver.switchTo().frame(frame); } WebElement grid = driver.findElement(By.xpath(".//*[@data-testid='data-grid-canvas']")); System.out.println(grid); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", grid); WebElement tr = driver.findElement(By.xpath( "//table[@role='grid']/descendant::tbody/tr/td[text()='st.balloons']/parent::tr")); String rowIndex = tr.getAttribute("aria-rowindex"); WebElement column = driver .findElement(By.xpath("//table[@role='grid']/descendant::thead/tr/th[text()='is_widget']")); String columnIndex = column.getAttribute("aria-colindex"); WebElement select = driver .findElement(By.xpath("//table[@role='grid']/descendant::tbody/tr[@aria-rowindex='" + rowIndex + "']/td[@aria-colindex='" + columnIndex + "']")); new Actions(driver).moveToElement(grid, select.getLocation().getX(), select.getLocation().getY()).click() .sendKeys(Keys.ENTER).build().perform(); } catch (Exception e) { e.printStackTrace(); } } }
内容的提问来源于stack exchange,提问作者SKV
相关产品推荐
相关产品推荐

