How to get label text from web table - selenium

I'm facing the problem with finding label text from table
I'm new to Selenium, help me understand what I'm doing wrong.
Thanks in advance
WebElement services = driver.findElement(By.xpath("//tr[#id='mr_2']/label"));
String strServices = services.getText();
System.out.println(strServices);
<tr class="alt" id="mr_2">
<td class="first_col">Menu Selection</td>
<td><label style="display: none" for="element_2_1">Poor</label>
<input id="element_2_1" name="element_2" type="radio" value="1"></td>
</tr>

Try changing the below line
String strServices = services.getText();
to
String strServices = services.getAttribute("textContent");

Related

Web Automation - How do I input text into a rich text box on a website (textarea) defined by a class?

I have been trying this for a few days and am completely stuck. If anybody could help me out I would be very grateful; Im using VB.NET.
The HTML Code:
<tbody>
<tr>
<td class="chat-attachment-cell"></td>
<td class="chat-input-cell">
<textarea class="chat-message-input"></textarea>
</td>
<td class="chat-send-cell"><a href="#" class="chat-send-button"> alt="Send" width="66" height="66" border="0">
</a>
</td>
</tr>
</tbody>
The text box I need to input into is this bit
<textarea class="chat-message-input"></textarea>
Thankyou in advance for any help provided
You select the element then change the .innertext property with what you want.
There are multiple ways to do it, and I can't give you an example because I don't know what you are using nor the whole html, but for example it could look like this:
WebBrowser1.Document.GetElementById("someid").InnerText="Sometext"
For start, you can try looking how the collection of elements you get looks, then you should be able to figure out what to do next.
Dim test = WebBrowser1.Document.GetElementsByTagName("textarea")
For example on this page:
Dim test = WebBrowser1.Document.GetElementsByTagName("tbody")
test(1).InnerText = "Hi there"

Selenium java to find the right element

I want to find the the element for the Edit:
my java code is not working.
String xpathLocater = "//a[contains(text(),'onEditFilter('modifier')')]";
return driver.findElement(By.xpath(xpathLocater));
This is the source code for the element.
<tr class="listeven">
<td>
<a onclick="return onEditFilter('modifier');" href="#">Edit</a>
</td>
</tr>
something like
String xpathLocater = "//a[contains(#onclick,\"onEditFilter('modifier')\")]"

How to retrieve the text from an outer element using selenium webdriver?

How to retrieve the text 'caught jake' from the below code using selenium webdriver?
Am able to point to that text using the below xpath but am unable to print the text. :(
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
<div class="row">
<div class="large 20 columns">
<table class="batting-table innings" width="100%">
<tbody>
<tr class="tr-heading">
<tr>
<tr class="dismissal-detail" style="display: table-row;">
<td width="2%" />
<td colspan="8">
<b>12.6</b> caught Jake
<b>73/4</b>
<br/>
Using java you can retrieve from following lines:
String text = driver.findElement(By.xpath("//div[#class='large 20 columns']")).getText();
System.Out.print("Text= "+text);
Above line will return all inner tags string's of div element.
If you want to get only 12.6 text, then use below lines:
String text = driver.findElement(By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")).getText();
System.Out.print("Text= "+text);
And be sure that there should be single element which has this xpath 'By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")', otherwise you will get an exception.
depend on your language, you can use following xpath expression:
//div[#class='large 20 columns']/table/tbody/tr/tr/td[2]/child::text()
full Java code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsx = "return document.evaluate(\"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;";
String objList = js.executeScript(jsx);
System.out.println( (String) objList);

How can i get src as an attribute

I need to getAttribute("src") of the image. When I run my script, it shows "null". BTW, action works!
My script:
WebElement image = driver.findElement(By.id("creativeLink"));
verifyDisplay(image.getAttribute("src"), By.id("creativeLink"));
action.moveToElement(image).perform();
Page HTML:
<div id="creativeContent">
<table>
<tbody><tr>
<td>
<a id="creativeLink" href="http://www.website.com" target="_new"> <img href="http://www.website.com" rel="faceboxMO" alt="" src="https://console.data.com/images/login/logo.png" height="50" border="0" width="50"></a>
</td>
<td valign="top"><div class="hint">(Note: Mouse over creative to see in correct dimensions)</div></td>
</tr>
<tr>
<td>
</td></tr></tbody></table>
</div>
Looks like you are using wrong selector there. Since you are using some custom function I was not able to test it. However, I came up with something like following and seems working. A correct selector should look like something as following:
// The selector finds the img element
By by = By.cssSelector("#creativeLink>img");
//Getting attribute on on img tag
String src = driver.findElement(by).getAttribute("src");
System.out.println(src);
Print
https://console.data.com/images/login/logo.png
Probably do something like this and it will work:
WebElement image = driver.findElement(By.cssSelector("#creativeLink>img"));
verifyDisplay(image.getAttribute("src"), By.cssSelector("#creativeLink>img"));
action.moveToElement(image).perform();

How can I fix this issue with my table?

I'm trying to replicate a one rowed table with a form, such that when I press the '+' button - I would like another table form to drop down below it exactly the way it is - however, when I do that and add the same element as the table with the HTML code, I get something different...
I got screenshot images to the form below and after that I got the code indented -
The code for the above images is listed below - the first being the forms and the second being the Javascript function
<body>
<form>
<div id="container1">
<table border="1" cellspacing="0" style="margin-top:12px width="900"">
<colgroup>
<col span="1">
</colgroup>
<tr>
<td valign="top">
<label for="company" size="3">Company:</label> <input type="text" id="company" name="company" maxlength="15" size="15">
</td>
<td valign="top">
<label for="position1" size="3">Position:</label> <input type="text" id="position1" name="position1" maxlength="20" size="12"> </td> <td valign="top"><label for="tasks" size="3"> Tasks: </label></td><td> <textarea id="tasks" name="tasks" maxlength="1000" cols="25" rows="1"></textarea></td>
<td valign="top"><label for="from1" size="3"> From: </label><input type="text" id="from1" name="from1" size="4" ></td> <td valign="top">To: <input type="text" id="to1" name="to1" size="4"> <td valign="top"><label for="location" size="3">Work Location: </label><input type="text" id="location" name="location" size="20" maxlength="25"> + <br></td>
</tr>
</table><br>
</div>
</form>
</body>
the javascipt code is posted below:
function aFields1(){
var container1 = document.getElementById("container1");
var table = document.createElement("table");
table.border=1;
table.cellspacing=0;
var tr = document.createElement("tr");
var td = document.createElement("td");
td.valign = "top";
var label = document.createElement("label");
label.for = "company";
label.size = 3;
container1.appendChild(document.createTextNode("Company: "));
container1.appendChild(label);
var company = document.createElement("input");
company.type = "text";
company.id = "company";
company.name = "company";
company.size = 15;
company.maxlenth = 15;
//append the company input element to the td element
td.appendChild(company);
tr.appendChild(td);
table.appendChild(tr);
//append the td element to the container1 element
//container1.appendChild(tr);
container1.appendChild(table);
Can anyone please tell me why the table is not showing up?
There should be a border with at least one cell with the company label and input bar INSIDE it - but as you can see from the image the Company label is on the top and the input bar with the border is below it...
where'd I go wrong? and why are the borders for the cell much much more different from what came out of the HTML code?
Part of the problem is that you're appending the label element to the DIV directly instead of appending it to the new td. This is why it's showing up outside of the Table.
From your last comment, it seems like what you really want to do is just append a cloned TR to the existing table. Ok, there are multiple ways to do this. You can completely re-build a duplicate tr in javascript, OR you can just clone the first table row and append it to the table. This is trivial with jQuery, but can be done with pure javascript.
First, let's add an id attribute to the table element. like so:
<table id="my_table" border="1" cellspacing="0" style="margin-top:12px;width:900px">
.....snip rest of your table....
</table>
Then your javascript cloning can work like this (note: code is untested):
function aFields1() {
var table, tr, cloned_row, inputs_to_clear;
// find the table
table = document.getElementById('my_table');
// find the first tr element
tr = table.querySelectorAll('tr')[0];
// clone the first tr using deep = true option to copy all children
tr_cloned = tr.cloneNode(true);
// NOTE: the values in the cloned row will already be prefilled, so you probably want to clear input-field values as well
inputs_to_clear = tr_cloned.querySelectorAll('input');
foreach (var input in inputs_to_clear) {
inputs_to_clear[input].value = '';
}
// append the new row
table.appendChild(cloned_row);
}