目前正在从事谷歌支付集成测试自动化工作,并遇到了元素位置问题。 Google 付款流程在单独的窗口中打开,我通过在会话中找到正确的句柄来管理该窗口
driver.switchTo().window(googlePayWindowHandle);
在我转移到需要单击“继续”按钮的页面之后,有一些技巧:
WebElement iframe = checkPresenceOfElementWithWait(driver, By.name("sM432dIframe"), WAIT_DURATION);
WebDriver frameDriver = driver.switchTo().frame(iframe);
WebDriverWait frameWait = new WebDriverWait(frameDriver, WAIT_DURATION);
WebElement element = frameWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@role='button']")));
但总是失败并出错
TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //div[@role='buttone']
我还尝试了另一种方法,例如使用 waitForElementPresence ,似乎 selenium 无法在 iframe 内找到该元素。为了确保该按钮位于框架内,我进入了 chrome 控制台并通过 xPath 进行搜索,它起作用了。
附注: 也尝试了 javascript 执行器,但仍然没有成功。
ele="//div[text()='Pay']";
js.executeScript("arguments[0].click();", ele);
我将元素作为javascript执行器点击传递,它有效,但不适用于普通点击方法