我有一堆以下作品:
WebElement L3_Y1_total_x = driver.findElement(By.xpath("//input[contains(@class,'sumCell blahblah total')]"));
String L3_Y1_total_x1_value = L3_Y1_total_x.getAttribute("value");
WebElement L3_C1 = driver.findElement(By.xpath("//input[contains(@class,'cell blahblah1 decimal')]"));
L3_C1.click();L3_C1.sendKeys("3,00");L3_C1.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y1_total_x, "value", L3_Y1_total_x1_value));} catch (TimeoutException e) {};
String L3_Y2_total_x1_value = L3_Y2_total_x.getAttribute("value");
WebElement L3_C2 = driver.findElement(By.xpath("//input[contains(@class,'cell cell blahblah2 decimal')]"));
L3_C2.click();L3_C2.sendKeys("3,00");L3_C2.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y2_total_x, "value", L3_Y2_total_x1_value));} catch (TimeoutException e) {};
String L3_Y3_total_x1_value = L3_Y3_total_x.getAttribute("value");
WebElement L3_C3 = driver.findElement(By.xpath("//input[contains(@class,'cell blahblah3 decimal')]"));
L3_C3.click();L3_C3.sendKeys("3,00");L3_C3.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y3_total_x, "value", L3_Y3_total_x1_value));} catch (TimeoutException e) {};
如何缩小代码?至少是 element.click();element.sendKeys("xy");...
序列。
谢谢
最佳答案
这样的东西应该适合你的目的(虽然我现在不能尝试)。使用 L3_Y1_total_x
、L3_Y2_total_x
和 L3_Y3_total_x
以及相应的参数 blahblah
调用该函数。
public void doAction(WebElement elem, String blahblah) {
String value = elem.getAttribute("value");
WebElement L3_C1 = driver.findElement(By.xpath("//input[contains(@class,'cell " + blablah + "decimal')]"));
L3_C1.click();
L3_C1.sendKeys("3,00");
L3_C1.sendKeys(Keys.TAB);
try {
wait10s.until(Attribute_Not_To_Be.attributeNotToBe(elem, "value", value));
} catch (TimeoutException e) {};
}
请注意,使用下划线和大写变量违反了广泛接受的 Java 编码约定。使用驼峰式大小写和小写变量。
关于java - selenium webdriver - 构造 Action 点击和发送键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48900722/