【问题标题】:How to send text to the amount field within the Angular Page using Selenium and Java如何使用 Selenium 和 Java 将文本发送到 Angular 页面中的金额字段
【发布时间】:2020-07-26 08:26:31
【问题描述】:

我想在下面的网址中输入金额值“123450”(例如)。

步骤-

  1. 转到网址link
  2. 点击顶部菜单栏上的“存款”
  3. 点击“定期存款>”
  4. 您将看到“定期存款”页面,金额字段默认为 10,000。

我想更改该字段并输入任何其他金额。

我确定的 Xpath -

//This works when the site loaded for the first time with 10,000 as the default value
@FindBy(xpath = "//div[@class='ieco-blue-underline']/span/b")
WebElement amountField;

//The XPath changes when we enter Amount as '500' <Tab out>..the XPath becomes as per below-
@FindBy(xpath = "//div[@class='ieco-blue-underline']/input")
WebElement amountField;

第一次输入时是否需要使用第一个 xpath..然后在输入 500,1000 之类的值时使用第二个 xpath?

我已经尝试了 3 种方法,但到目前为止都没有奏效 -

public static void sendKeysWithWait(WebElement element, String text){
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(element));
  1. element.click(); 元素.清除(); //element.sendKeys(Keys.DELETE); element.sendKeys(文本); //按Tab键 element.sendKeys(Keys.TAB);*/

  2. Actions 动作 = new Actions(driver); 动作.moveToElement(元素); 动作.click(); actions.sendKeys(text); action.build().perform(); //按Tab键 element.sendKeys(Keys.TAB);

  3. element.click(); JavascriptExecutor jse = (JavascriptExecutor)驱动程序; jse.executeScript("arguments[0].value='2222';", element);

在这些方式中,Click 都可以正常工作..但是 sendkeys 不能正常工作。 我正在使用 chromedriver 执行,Selenium 版本 3.141.59

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要将字符序列 123450 发送到 webpage 内的 Amount 字段,因为该元素是 Angular 元素,您需要使用 WebDriverWait elementToBeClickable(),您可以使用以下任一Locator Strategies:

    • cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.ieco-invest-rb20 span.ng-star-inserted>b"))).click();
      WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='investedAmt']")));
      element.clear();
      element.sendKeys("123450");
      
    • xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'ieco-invest-rb20')]//span[@class='ng-star-inserted']/b"))).click();
      WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='investedAmt']")));
      element.clear();
      element.sendKeys("123450");
      
    • 浏览器快照:

    【讨论】:

      【解决方案2】:

      要回答您的第一个问题,您的第一个 xpath 是错误的,因为事件绑定到该 div。

      你的 xpath 应该是

      @FindBy(xpath = "//div[@class='ieco-blue-underline']")
      WebElement amountField;
      

      然后尝试使用第一种使用click的方法。 希望这有效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多