0

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");
11
  • You want to get Hotel Creek? Commented Feb 20, 2015 at 5:18
  • yes Saifur, the requirement is to validate this text, which is dynamic. Commented Feb 20, 2015 at 5:19
  • The id="hotel_name_0" is also dynamic? Commented Feb 20, 2015 at 5:20
  • added snapshot of html code Commented Feb 20, 2015 at 5:26
  • @Saifur, it is also dynamic Commented Feb 20, 2015 at 5:27

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Saifur, but there could be more than one row based on user search criteria.
Provide me more html or url to the site so I can write you a unique selector
0

Just do

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

Hope it helps

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.