当动态生成Value属性时,如何在Selenium Web驱动程序中使用java从标记获取值

时间:2022-05-16 08:45:18

The HTML I am trying to operate on:

我试图操作的HTML:

<select id="GlobalDateTimeDropDown" class="combo"
    onchange="CalculateGlobalDateTime('Time',this.value)" name="GlobalDateTimeDropDown">
       <option value="+5.5" selected="selected"> … </option>
       <option value="+12"> … </option>
       <option value="+8"> … </option>
       <option value="+2"> … </option>
    </select>
    <input id="GlobalDateTimeText" class="combo" type="text" name="GlobalDateTimeText"       value="" style="width:215px;padding:2px;" readonly="readonly"></input>

Java Code:

Java代码:

WebElement values=driver.findElement(By.id("GlobalDateTimeText")).getAttribute("Value");
System.out.println(values);

Output: Blank

输出:空白

2 个解决方案

#1


6  

To select the input value you'll need to use an xpath selector as using by.id will find the select element as they share the same id - which is bad practice as they really should be unique. Anyway, try:

要选择输入值,您需要使用xpath选择器,因为使用by.id将找到select元素,因为它们共享相同的id - 这是不好的做法,因为它们确实应该是唯一的。无论如何,尝试:

driver.findElement(By.xpath("//input[@id='GlobalDateTimeText']")).getAttribute(‌​"value");

#2


0  

Code should be something like this

代码应该是这样的

WebElement dropDown = driver.findElement(By.id("GlobalDateTimeText"));
Select sel = new Select(dropDown);
List<WebElement> values = sel.getOptions();
for(int i = 0; i < values.size(); i++)
{
   System.out.println(values.get(i).getText());
}

#1


6  

To select the input value you'll need to use an xpath selector as using by.id will find the select element as they share the same id - which is bad practice as they really should be unique. Anyway, try:

要选择输入值,您需要使用xpath选择器,因为使用by.id将找到select元素,因为它们共享相同的id - 这是不好的做法,因为它们确实应该是唯一的。无论如何,尝试:

driver.findElement(By.xpath("//input[@id='GlobalDateTimeText']")).getAttribute(‌​"value");

#2


0  

Code should be something like this

代码应该是这样的

WebElement dropDown = driver.findElement(By.id("GlobalDateTimeText"));
Select sel = new Select(dropDown);
List<WebElement> values = sel.getOptions();
for(int i = 0; i < values.size(); i++)
{
   System.out.println(values.get(i).getText());
}