首页 > 解决方案 > 在 Angular 站点中,即使 xpath 正确,元素也无法找到错误显示。尝试使用 javascript 执行器仍然无法正常工作

问题描述

输入信用卡号时,它不在框架中并引发错误。请注意,这是一个基于角度的网站。

driver.findElement(
    By.xpath("//input[@id='credit-card-number']")
).sendKeys("4111111111111111");

控制台错误

FAILED: orderplace
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='credit-card-number']"}
  (Session info: chrome=84.0.4147.89)

标签: javaseleniumautomation

解决方案


尝试使用此代码代码:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement myinput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='credit-card-number']")));
myinput.sendKeys("4111111111111111");

我添加了wait'直到输入框可见。一旦它可见,您就可以将文本放入其中。


推荐阅读