我有一个场景,我需要在文本字段中发送值。值为1或0,具体取决于当前的星期几。在下面的场景中,我需要输入' 0' 0当天和' 1'其余的日子。我希望,从星期二开始,我会得到" 0"在星期二文本字段中,但它在所有文本字段中显示为0。以下是我从sunday - tuesday
开始的代码。
顺便说一句:id=w_1
是星期日,w-2
是星期一,w_3
是星期二。
谢谢!
public String enter0And1(String daysOfWeek) {
Calendar calendar = Calendar.getInstance();
int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
WebElement sun = driver.findElement(By.id("w_1"));
sun.sendKeys(Keys.CONTROL + "a");
sun.sendKeys(Keys.DELETE);
if(driver.findElement(By.id("w_1")).equals(currentDay)){
sun.sendKeys(current_day_zero);
}
else {
sun.sendKeys(current_day_one);
}
WebElement mon = driver.findElement(By.id("w_2"));
mon.sendKeys(Keys.CONTROL + "a");
mon.sendKeys(Keys.DELETE);
if(driver.findElement(By.id("w_2")).equals(currentDay)){
mon.sendKeys(current_day_zero);
}
else {
mon.sendKeys(current_day_one);
}
WebElement tues = driver.findElement(By.id("w_3"));
tues.sendKeys(Keys.CONTROL + "a");
tues.sendKeys(Keys.DELETE);
if(driver.findElement(By.id("w_3")).equals(currentDay)){
tues.sendKeys(current_day_zero);
}
else {
tues.sendKeys(current_day_one);
}
return daysOfWeek;
}
星期二的HTML :
<tr>
<td><div width="60%" class="form_label" id="w_3_label">
Tuesday
</div>
</td>
<td><input type="text" id="w_3" onchange="calculateAvgDI()" value="" size="9" maxlength="9" name="dailyWeightGoal(tuesday)">
</td>
答案 0 :(得分:1)
问题是您正在尝试将WebElement
与currentDay
int进行比较。而是在getAttribute()
的帮助下使用实际输入值。
替换:
if(driver.findElement(By.id("w_1")).equals(currentDay)){
sun.sendKeys(current_day_zero);
}
else {
sun.sendKeys(current_day_one);
}
使用:
int value = Integer.parseInt(driver.findElement(By.id("w_1").getAttribute('value'));
sun.sendKeys(value == currentDay ? current_day_zero : current_day_one);
答案 1 :(得分:1)
Alecxe已经指出了你所遇到的问题。作为替代方案,您可以尝试使用这段代码。
String day = new Date().toString().split(" ")[0]; //returns day of week as for eg. Wed
List<WebElement> days = driver.findElements(By
.xpath("//input[contains(@name,'dailyWeightGoal')]")); // Will get list of all input elements
for (WebElement webElement : days) {
if (webElement.getAttribute("name").contains(day.toLowerCase())) // since the attribute name contains day,
//so checking like wise according to scenario
webElement.sendKeys("0");
else
webElement.sendKeys("1");
}