Selenium Webdriver 无法获取 Modal 中的元素

roh*_*2sh 6 selenium modal-dialog selenium-webdriver

我正在编写一个 Selenium Webdriver 脚本,它应该点击一个链接,然后弹出这个模态窗口。

页面上的模态窗口

当我尝试访问卡号字段 (//input[@id=pan]) 时,出现 No such element found 异常 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"pan"}

我想掌握的输入控件

这是我尝试过但没有运气的代码:

WebElement modal = driver.findElement(By.xpath("//div[@class='ute-pay-now-modalContent']"));
driver.switchTo().frame(modal);
WebElement el =  driver.findElement(By.xpath("//input[@id='pan']"));
Run Code Online (Sandbox Code Playgroud)

也试过这个:

WebElement modal = driver.findElement(By.className("ute-pay-now-modalContent"));
driver.switchTo().frame(modal);
WebElement el =  driver.findElement(By.xpath("//input[@id='pan']"));
Run Code Online (Sandbox Code Playgroud)

也试过这个:

WebDriverWait block = new WebDriverWait(driver,10);
WebElement modal = block.until(ExpectedConditions.visibilityOfElementLocated(By.className("ute-pay-now-modalContent")));
WebElement pan;
pan    = modal.findElement(By.id("pan"));
Run Code Online (Sandbox Code Playgroud)

也试过这个:

driver.switchTo().defaultContent();
Run Code Online (Sandbox Code Playgroud)

也试过这个:

driver.switchTo().activeElement();
Run Code Online (Sandbox Code Playgroud)

有人可以帮我建议如何解决这个问题吗?

And*_*son 6

似乎<div class="ute-pay-\xe2\x80\x8c\xe2\x80\x8bnow-modalContent">包含iframe#sema必需的input字段。尝试下面的代码并让我知道结果:

\n\n
WebDriverWait block = new WebDriverWait(driver,10);\nblock.until(ExpectedConditions.visibilityOfElementLocated(By.className("ute-pay-now-modalContent")));\ndriver.switchTo().frame("sema");\nWebElement pan;\npan = modal.findElement(By.id("pan"));\n
Run Code Online (Sandbox Code Playgroud)\n

  • 您可以检查“input”是否是“&lt;iframe id="sema"&gt;”的后代。这是嵌入的“HTML”文档。要处理“iframe”内的元素,您必须首先切换到它。如果在处理“input”之后您需要处理“iframe”之外的其他元素,您应该切换回主文档“driver.switchTo().defaultContent();”。 (2认同)