PrateekSethi
PrateekSethi

Reputation: 56

How to retrieve the value of an attribute using Seleium WebDriver?


enter image description hereThe HTML code is given attached, I do not want to use hard code xpath, the requirement is to make it generic:

<td bgcolor="#FFFFFF">
  <input name="hotel_name_0" id="hotel_name_0" type="text" value="Hotel Creek" class="select_text" onfocus="disable_ctrlV()" onkeypress="return Nothingonly(event)">
</td>

Code:

public static boolean fncVerifyTextInColumn(WebElement gridObjWebElement,
            String stringToValidate, int columnNumber,String colName) {
        boolean flagTextinColumn=false;
        ArrayList<WebElement> objRows;
        ArrayList<WebElement> objCols;
        ArrayList<WebElement> childElement;
        objRows=(ArrayList<WebElement>)gridObjWebElement.findElements(By.tagName("tr"));
        objCols=(ArrayList<WebElement>)objRows.get(0).findElements(By.tagName("td"));
        if(objCols.get(columnNumber).getText().equalsIgnoreCase(colName)){
            for(int index=1;index<objRows.size();index++){
                objCols=(ArrayList<WebElement>)objRows.get(index).findElements(By.tagName("td"));
                childElement=(ArrayList<WebElement>)objCols.get(columnNumber).findElements(By.xpath("//input"));
                System.out.println(childElement.get(0).getAttribute("value"));
                if(stringToValidate.trim().equalsIgnoreCase(childElement.get(0).getAttribute("value").trim())){
                    flagTextinColumn=true;
                }
            }
        }
        return flagTextinColumn;
    }

Method Calling:

fncVerifyTextInColumn(objGrid,hotels,1,"Hotel Name");

Upvotes: 0

Views: 3265

Answers (3)

Dave Bush
Dave Bush

Reputation: 2402

I think what you are looking for is this. (I'm assuming you know how to code, you just need a general direction so I'm going to leave out specific code.)

First, find the table the td is in. You might need to use an xPath for this or you'll need to assign an ID to the table so you can locate it.

Then once you have the table, do a FindElements to get the list of TRs under it.

Once you have the TRs, you can loop through them, grab the TDs under that and grab the TD at the index that has the INPUT you want the value of, get the INPUT and then get it's value.

Yep, lots of steps.

A shortcut may be to class all of the inputs you want the value for with a unique class and do a FindElements By className and loop through that list.

Upvotes: 0

Mayur Shah
Mayur Shah

Reputation: 528

Just do

String attValue = driver.findElement(byAnyMethod).getAttribute("AttributeName");

Hope it helps

Upvotes: 0

Saifur
Saifur

Reputation: 16201

I would use cssSelector [id^='hotel_name_'] to locate the element and then getAttribute() retrieve the attribute value

By css = By.cssSelector("[id^='hotel_name_']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(css));
System.out.println(myDynamicElement.getAttribute("value"));

Notice the regex search of cssSelector here. With ^ I am skipping any dynamic number. Hoping that's the only element with hotel_name_someNumber on the page.

Upvotes: 2

Related Questions