如何使用 Selenium 和 Java 将文本发送到 Angular 页面中的金额字段
How to send text to the amount field within the Angular Page using Selenium and Java
我想在下面输入金额为“123450”(例如)url。
步数 -
- 转到URLlink
- 点击顶部菜单栏上的'Deposits'
- 点击'Fixed Deposit >'
- 您将看到金额字段默认为 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 种方法,但 none 到目前为止都有效 -
public static void sendKeysWithWait(WebElement element, String text){
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(element));
element.click();
element.clear();
//element.sendKeys(Keys.DELETE);
element.sendKeys(文字);
//按Tab键
element.sendKeys(Keys.TAB);*/
Actions actions = new Actions(driver);
actions.moveToElement(元素);
actions.click();
actions.sendKeys(文字);
actions.build().perform();
//按Tab键
element.sendKeys(Keys.TAB);
element.click();
JavascriptExecutor jse = (JavascriptExecutor)驱动程序;
jse.executeScript("参数[0].value='2222';", 元素);
在上述每一种方式中,Click 都有效..但 sendkeys 无效。
我正在使用 chromedriver 执行,Selenium 版本 3.141.59
要回答您的第一个问题,您的第一个 xpath 是错误的,因为事件绑定到 div。
你的xpath应该是
@FindBy(xpath = "//div[@class='ieco-blue-underline']")
WebElement amountField;
然后尝试使用第一种使用点击的方法。
希望这有效
将字符序列 123450 发送到 webpage, as the element is an Angular element you need to use for the elementToBeClickable()
and you can use either of the following 中的 Amount 字段:
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");
浏览器快照:
我想在下面输入金额为“123450”(例如)url。
步数 -
- 转到URLlink
- 点击顶部菜单栏上的'Deposits'
- 点击'Fixed Deposit >'
- 您将看到金额字段默认为 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 种方法,但 none 到目前为止都有效 -
public static void sendKeysWithWait(WebElement element, String text){
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(element));
element.click(); element.clear(); //element.sendKeys(Keys.DELETE); element.sendKeys(文字); //按Tab键 element.sendKeys(Keys.TAB);*/
Actions actions = new Actions(driver); actions.moveToElement(元素); actions.click(); actions.sendKeys(文字); actions.build().perform(); //按Tab键 element.sendKeys(Keys.TAB);
element.click(); JavascriptExecutor jse = (JavascriptExecutor)驱动程序; jse.executeScript("参数[0].value='2222';", 元素);
在上述每一种方式中,Click 都有效..但 sendkeys 无效。 我正在使用 chromedriver 执行,Selenium 版本 3.141.59
要回答您的第一个问题,您的第一个 xpath 是错误的,因为事件绑定到 div。
你的xpath应该是
@FindBy(xpath = "//div[@class='ieco-blue-underline']")
WebElement amountField;
然后尝试使用第一种使用点击的方法。 希望这有效
将字符序列 123450 发送到 webpage, as the element is an Angular element you need to use elementToBeClickable()
and you can use either of the following
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");
浏览器快照: