cooler8020002000
cooler8020002000

Reputation: 39

Using selenium webdriver to find the label text

I was having a hard time using Selenium web driver to find the label text for the below scenario.

<tr id="row"> 
<td width="148" height="22" align="left"> 
<b> Label Text</b> 
</td> </tr>

How do I get the Label Text using webdriver ?

Thanks in advance.


Thanks for the reply. I think I should have been more specific. I have a list of td tags inside the tr tag. For eg:

<tr id="row"> 
<td width="148" height="22" align="left"> 
<b> Label Text 1</b> 
<td width="148" height="22" align="left"> 
<b> Label Text 2</b> 

<td width="148" height="22" align="left"> 
<b> Label Text 3</b> 
</td> </tr>

driver.findElement(By.id("row")).getText() would retrieve me all of those label values. Can I get the label value based on the location in the page ? Thanks.

Upvotes: 1

Views: 49991

Answers (4)

charelf
charelf

Reputation: 3855

captcha = driver.find_element(by=By.XPATH, value=captcha_xpath)
print(captcha.text)

Upvotes: 0

Hari Reddy
Hari Reddy

Reputation: 3858

You can loop through all required labels one by one by using the following code -

List<WebElement> list = driver.findElements(By.xpath("//*[@id='row']/td/b"));

for(int i=0;i<list.size;i++){

   list.get(i).getText();

  }

I hope that this answers your problem.

Upvotes: 1

Nashibukasan
Nashibukasan

Reputation: 2028

To get a value via its location you can use something like:

    ReadOnlyCollection<IWebElement> cells = driver.findElements(By.XPath("//tr[@id='row']//td"));
    foreach(IWebElement cell in cells)
    {
        if (cell.Location.X == targetX && cell.Location.Y == targetY)
        {
            cell.Text;
        } 
    }

Also, FindElement will only return the first element found that matches the criteria, FindElements must be used to store all matches.

Upvotes: 0

Mike Kwan
Mike Kwan

Reputation: 24477

driver.findElement(By.id("row")).getText();

Upvotes: 2

Related Questions