Selenium WebDriver-构造操作单击和发送keys



我有很多以下部分:

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_xL3_Y2_total_xL3_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编码约定。使用骆驼套和小写变量。

最新更新