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

Can we access web elements of bank websites like ICICI, HDFC, SBI etc for automation using selenium Webdriver?

我正在尝试通过访问用户名、联系电话、电子邮件等表单的 Web 元素来自动化银行网站。但是当我尝试 运行 时,我无法访问它的 Web 元素,因为它因异常 "unable to locate an element" 而失败,让我知道我们如何访问它。

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

您的问题分为两个部分,一个技术部分得到了回答,您的元素位于 iframe 中,因此您的自动化代码无法找到它。

你问题的第二部分(当你列出这么多银行时)是关于合法性以及特定银行是否允许这样做。一般的经验法则是——Selenium 用于自动测试您正在正式开发的基于 Web 的应用程序(您自己的应用程序、您组织的应用程序等)。所以在我看来,如果你没有得到这些银行的授权,那么你不应该这样做。

如果不违法,那么至少有可能会阻止用户 ID/帐户,因为大多数大公司都采用机器人检测算法。如果他们在您最初的请求时阻止您(并且只有您的 IP 被阻止),那很好,但是如果您被允许继续前进直到输入 ID 和密码等……那么这对您来说太冒险了。这些网站专为人类使用而设计,自动化可能会导致拒绝服务攻击(包括大量其他类型的攻击),因此大多数组织会检测并阻止此类请求。