java - 我们可以使用 selenium Webdriver 访问 ICICI、HDFC、SBI 等银行网站的 Web 元素以实现自动化吗?

标签 java selenium iframe webdriver ui-automation

我正在尝试通过访问用户名、联系电话、电子邮件等表单的 Web 元素来自动化银行网站。但是当我尝试运行时,我无法访问它的 Web 元素,因为它失败了,但有异常“无法找到某个元素”,请告诉我如何访问它。

icicibank.com/Personal-Banking/loans/personal-loan/index.page

代码:

WebElement firstName = driver.findElement(By.xpath("//*[@id=\"firstNameId\"]")); 
WebElement lastName = driver.findElement(By.id("lastNameId"));

最佳答案

您的元素位于 iframe 内。您必须切换到它才能找到元素:

WebElement iframe = driver.findElement(By.xpath("//iframe"));
driver.switchTo().frame(iframe);

然后您可以像往常一样找到您的元素:

WebElement firstName = driver.findElement(By.xpath("//*[@id='firstNameId']")); 
WebElement lastName = driver.findElement(By.xpath("//*[@id='lastNameId']"));

完成 iframe 中的内容后,您必须切换回默认内容,如下所示:

driver.switchTo().defaultContent();

PS:我也会像这样使用WebDriverWait:

WebDriver driver = new ChromeDriver();
driver.get("https://www.icicibank.com/Personal-Banking/loans/personal-loan/index.page");

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='myDivAdd']/a[2]"))).click(); // dismiss popup

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe"))); // switch to iframe

WebElement firstName = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='firstNameId']")));
firstName.click();
firstName.sendKeys("first name");

WebElement lastName = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lastNameId']")));
lastName.click();
lastName.sendKeys("last name");

driver.switchTo().defaultContent();

WebDriverWait 将等待至少 10 秒,直到满足 ExpectedConditions 后才执行操作。

注意:您必须添加一些导入:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;

有关WebDriverWait的更多信息可以在 documentation 中找到。 .

关于java - 我们可以使用 selenium Webdriver 访问 ICICI、HDFC、SBI 等银行网站的 Web 元素以实现自动化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51558381/

相关文章:

java - 如何将表示为两个非空链表的两个整数相加?

javascript - 从 iframe 本身中删除父 iframe

css - 使用 Selenium IDE 测试元素高度

css - Selenium CSS 选择器 :visible is not a valid selector

javascript - 如何在两个 iframe 中获取元素的属性

html - IFrame 的默认宽度/高度

java - Java中的无锁并发链表

java - 从匿名内部类中获取封闭类对象作为函数参数

java - 有效地找到排序数组中的整数数量

python - 使用 selenium 和 python 从 Iframe 获取文本