I am new to vb.net and need some help. I'm trying to autofill some fields on a website and press the send button, and I know how to do it when there is an ElementID:
WebBrowser1.Document.GetElementById("value")
But I'm stuck now on this webpage which has no ElemendIDs.
The HTML code is:
<tr>
<td align="center">
<br>
<br>
<form method="POST">
<table class="form">
<tr>
<td width="100">Login*</td>
<td><input type="text" name="login"></td>
</tr>
<tr>
<td>Password*</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>Full name</td>
<td><input type="text" name="full_name"></td>
</tr>
<tr>
<td>Account number</td>
<td><input type="text" name="account_number"></td>
</tr>
<tr>
<td>MAC</td>
<td><input type="text" name="stb_mac"></td>
</tr>
<tr>
<td>Account disabled</td>
<td><input type="checkbox" name="status" value="0"></td>
</tr>
<tr>
<td>Tariff plan</td>
<td>
<select name="tariff_plan_id">
<option value="0">---</option>
<option value="1">Full</option> </select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Add"/></td>
</tr>
</table>
</form>
</td>
</tr>
How do I find the right values to write inside these fields? Also, how can I select <option value="1">Full</option> and then press the button to send the autofilled values to site?
Thank you for your help
EDIT:
#VisualVincent I tried your third example and managed to find:
<table class="form">
I'm now stuck on how to find this child:
<td><input type="text" name="login"></td>
This is what I tried:
If Element.GetAttribute("className") = "form" Then
Console.WriteLine("found form")
'Parent found.
'Inner loop, looking for the child element we want (<option value="5">Banana</option>).
For Each OptionElement As HtmlElement In Element.GetElementsByTagName("td")
If OptionElement.GetAttribute("name") = "login" Then
'Found. Do something with 'OptionElement'...
Console.WriteLine("login found")
ElementFound = True
Exit For 'Exit the inner loop.
End If
Next
End If
I also tried to find the other elements, for instance:
For Each OptionElement As HtmlElement In Element.GetElementsByTagName("input")
If OptionElement.GetAttribute("name") = "stb_mac" Then
Element.SetAttribute("value", variable(4))
ElementFound = True
Exit For 'Exit the inner loop.
End If
Next
And for some reason that doesn't work, but this does:
For Each Element As HtmlElement In WebBrowser1.Document.All 'Iterate all <select> tags. You can use Document.All here as well.
If Element.GetAttribute("name") = "login" Then Element.SetAttribute("value", variable(0))
If Element.GetAttribute("name") = "password" Then Element.SetAttribute("value", variable(1))
If Element.GetAttribute("name") = "full_name" Then Element.SetAttribute("value", variable(2))
If Element.GetAttribute("name") = "account_number" Then Element.SetAttribute("value", variable(3))
If Element.GetAttribute("name") = "stb_mac" Then Element.SetAttribute("value", variable(4))
Next
You're almost there. The only problem is here:
For Each OptionElement As HtmlElement In Element.GetElementsByTagName("td")
If OptionElement.GetAttribute("name") = "login" Then
You're simply iterating the wrong element type ("tag"). You're iterating <td> elements, whereas name="login" is applied to an <input> element.
Change the loop to the following, and it should work:
For Each OptionElement As HtmlElement In Element.GetElementsByTagName("input")
As a side note I also recommend you change the variable name to something which better describes what it actually is. OptionElement was used in my example because I iterate <option> elements, so in this case I'd name it InputElement instead:
For Each InputElement As HtmlElement In Element.GetElementsByTagName("input")
Then to programmatically put text in the input box/text box, simply use the SetAttribute() method to modify the value attribute:
InputElement.SetAttribute("value", "someTextHere")
Related
I can successfully get innertext of <td> tags for both <table class="specs" ng-hide="false"> and <table class="specs ng-hide" ng-hide="true"> with this:
Set CHRA = aIE.document.getElementsByClassName("specs")(4).getElementsByTagName("td")
For Each Item In CHRA
Debug.Print Item.innertext
Next
These true and false html are from different urls, where one table is hidden and the other is visible to user. For True one, outputs are CHRA and 779543-0027, For False one, outputs are CHRA and 715214-0011:
I don't want to get the result for the false property. Is there any way that I can bypass this?
Here is the rest of the html code (true version is exactly the same except first part is <table class="specs ng-hide" ng-hide="true">:
<table class="specs" ng-hide="false">
<thead>
<tr>
<th colspan="2">BOM Info</th>
</tr>
</thead>
<tbody>
<tr>
<td>CHRA</td>
<td><span class="ng-binding">715214-0011</span> <input type="checkbox" ng-hide="bom.strChra[0]==undefined" id="cpbchra" class="combuy" value="715214-0011" style="display: none;"></td>
</tr>
</tbody>
</table>
I found the solution in case you are interested:
Set CHRA1 = aIE.document.getElementsByClassName("specs ng-hide")(0).getElementsByTagName("td")
Set CHRA2 = aIE.document.getElementsByClassName("specs")(4).getElementsByTagName("td")
If CHRA1(1).innertext <> CHRA2(1).innertext Then
Debug.Print CHRA2(1).innertext
End If
I need to click on a button in IE. I tried few options with no success. Everything i tried are using TagName/ ClassName/ Name/ ID. But I don't find any of these in the below.
<FORM method=post name=frmSearch action=searchResultsSummary>
<INPUT type=hidden value=GMT+01:00 name=clitz>
<DIV style="MARGIN-LEFT: 30px">
<DIV style="MARGIN-BOTTOM: 10px; FONT-WEIGHT: bold">Common Criteria</DIV>
<TABLE style="MARGIN-LEFT: 20px" cellSpacing=5 cellPadding=0 border=0>
<TBODY>
<TR>
<TD id=oDocumentCategoryCell style="HEIGHT: 22px" colSpan=3 noWrap>Document category:</TD>
<TD colSpan=3 align=left>
<SELECT style="DISPLAY: block" name=docCategory old_value="2">
<OPTION value=%>Any Category</OPTION>
<OPTION value=FINANCE>Finance Document</OPTION>
<OPTION selected value=INBOUND>Inbound Document</OPTION>
<OPTION value=PROCESS>Internal Process</OPTION>
<OPTION value=OUTBOUND>Outbound Document</OPTION>
</SELECT>
</TD>
<TD style="WIDTH: 15px"></TD>
<TD>
<INPUT onclick="javascript:if (!validateSearchForm()) return;if (!checkCountOfDays()) return; addRefKeysToForm(frmSearch); frmSearch.submit();" style="WIDTH: 100px" type=button value=Search>
</TD></TR>
<TR>
<TD style="HEIGHT: 22px" colSpan=3 noWrap>Document type:</TD>
<TD colSpan=3 align=left>
<SELECT style="DISPLAY: block" name=docType old_value="0">
<OPTION selected value=%>Any Type</OPTION>
<OPTION value=CSN_ORD_VALID>CSN Order Validation</OPTION>
<OPTION value=CREDIT_ORD>Credit Order</OPTION>
<OPTION value=EOI_ORDCPY>EOI Order Copy</OPTION>
<OPTION value=LEASE_ORD>Lease Order</OPTION>
<OPTION value=ORDER_REPORT>Order Load Report</OPTION>
<OPTION value=TRADE_ORD>Trade Order</OPTION>
<OPTION value=WATSON>WATSON Quote</OPTION>
<OPTION value=WATSON_UPDATE>WATSON Update Quote</OPTION>
<OPTION value=WNGQ>WNGQ Request</OPTION></SELECT> </TD></TR>
<TR>
I tried going by input tags and it is not working.
I used the below code. It clicks on the button i want but it is not considering the values i gave before clicking on submit button.
Dim btnInput As MSHTML.HTMLInputElement
Dim frm As MSHTML.IHTMLElementCollection
Application.ScreenUpdating = True
Set frm = ie.Document.getElementsByName("frmSearch")
For Each btnInput In frm
If btnInput.Value = "frmSearch.submit()" Then
btnInput.submit
Exit For
End If
Next btnInput
Can some one help me on how to click on the button.
As #Tim Williams suggest, this should work document.querySelector("input[value=Search]").
If this still doesn't return the correct input element then the selector can be made more specific according to the DOM tree where the searched input is located, e.g. like this.
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
Dim inputElement As HTMLInputElement
Set inputElement = doc.querySelector( _
"div[class=main] div[id=inner] table input[type=button][value=Search]")
If Not inputElement Is Nothing Then
inputElement.Click
End If
HTML
<div class="main">
<div id="inner">
<table>
<tbody>
<tr>
<td>
<INPUT onclick="alert('This is the corect one');"
style="WIDTH: 100px"
type=button
value=Search>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<INPUT onclick="alert('No this is not the searched one');"
style="WIDTH: 100px"
type=button
value=Search>
EDIT:
Not sure if I understand your problem correctly.
To select the INPUT element this selector can be used:
Set inputElement = doc.querySelector( _
"form[name=frmSearch] table tbody tr td input[type=button][value=Search]")
To set the value attribute setAttribute() function can be used. To check value attribute getAttribute() function can be used.
If Not inputElement Is Nothing Then
inputElement.setAttribute "value", "some-new-value"
If inputElement.getAttribute("value") = "some-new-value" Then
inputElement.Click
End If
End If
This HTML code has menu of targets and you need to select one target. One of these targets is ANY TARGET to continue process.
<tr id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0" class=" iceRowSelMouseOver" tabindex="0" onmouseover="this.className=' iceRowSelMouseOver';" onmouseout="Ice.enableTxtSelection(document.body); this.className='iceDatTblRow1 selectable-rowRow1 iceRowSel'" onmousedown="return Ice.preventTextSelection(event);" ondblclick="Ice.registerDblClick(this);" onclick="Ice.registerClick(this,'mainForm:nav-panel-tab-set:0:trigger-selectTableclick_row','mainForm:nav-panel-tab-set:0:trigger-selectTableclick_count','0','mainForm',200,true,event,false,'mainForm:nav-panel-tab-set:0:trigger-selectTablesel_rows','');">
<td class="iceDatTblCol1 selectable-rowCol1" scope="row">
<a id="mainForm:nav-panel-tab-set:0:trigger-selectTable_idx_0" class="iceHdnLnk" onfocus="return Ice.tblRowFocus(this, false);" onblur="return Ice.tblRowBlur(this);" href="#">
<img alt="" src="/ice/xmlhttp/css/xp/css-images/spacer.gif"/>
</a>
<span id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1417" class="iceOutTxt">ANY TARGET</span>
</td>
<td class="iceDatTblCol2 selectable-rowCol2">
<span id="mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1419" class="iceOutTxt select-icon">ยป</span>
</td>
</tr>
How can I reach the ANY TARGET to click on >> to add using XPATH?
Extra Information:
The previous code is for sub window to add target. the main window code is:
<tr class="icePnlGrdRow1 settings-tableRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1305-2-0" class="icePnlGrdCol1 settings-tableCol1">
<table id="mainForm:nav-panel-tab-set:0:j_idt1326" class="icePnlGrd settingsLabel">
<tbody>
<tr class="icePnlGrdRow1 settingsLabelRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1326-0-0" class="icePnlGrdCol1 settingsLabelCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1327" class="iceOutTxt label">Target</span>
</td>
</tr>
<tr class="icePnlGrdRow2 settingsLabelRow2">
<td id="mainForm:nav-panel-tab-set:0:j_idt1326-1-0" class="icePnlGrdCol1 settingsLabelCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1328" class="iceOutTxt sidenote">Target or group</span>
</td>
</tr>
</tbody>
</table>
</td>
<td id="mainForm:nav-panel-tab-set:0:j_idt1305-2-1" class="icePnlGrdCol2 settings-tableCol2">
<table id="mainForm:nav-panel-tab-set:0:j_idt1329" class="icePnlGrd search">
<tbody>
<tr class="icePnlGrdRow1 searchRow1">
<td id="mainForm:nav-panel-tab-set:0:j_idt1329-0-0" class="icePnlGrdCol1 searchCol1">
<span id="mainForm:nav-panel-tab-set:0:j_idt1330" class="iceOutTxt"/>
</td>
<td id="mainForm:nav-panel-tab-set:0:j_idt1329-0-1" class="icePnlGrdCol2 searchCol2">
<input id="mainForm:nav-panel-tab-set:0:j_idt1331" class="iceCmdBtn" type="image" src="/ice/img/search-icon.png" onfocus="setFocus(this.id);" onclick="iceSubmitPartial(form, this, event);return false;" onblur="setFocus('');" name="mainForm:nav-panel-tab-set:0:j_idt1331"/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
I used ID to click on the targets to open the sub window
//*[#id='mainForm:nav-panel-tab-set:0:j_idt1331']
However, I did not get any response to add any target to my main window either I used the solutions you mentioned.
You can reach the span containing "ANY TARGET":
//span[text() = "ANY TARGET"]
You can extend this to reach the span containing ">>" in the td immediately following the td that contains "ANY TARGET":
//td[span[text() = "ANY TARGET"]]/following-sibling::td[1]/span[text() = ">>"]
Try to focus on the first TD and then click on the Span element.
IWebElement td = browser_drive.FindElement(By.Id("mainForm:nav-panel-tab-set:0:trigger-selectTable:0")).FindElements(By.TagName("td"))[0];
IWebElement anytext = td.FindElement(By.Id("mainForm:nav-panel-tab-set:0:trigger-selectTable:0:j_idt1417"));
OpenQA.Selenium.Interactions.Actions act = new OpenQA.Selenium.Interactions.Actions(browser_drive);
act.MoveToElement(td).Click(anytext);
(Coded using C#, there must be an alternative. Pseudo code, not complied)
I'm trying to use Excel VBA to perform a very manual process in IE. I've worked with VBA and IE enough to do easy tasks like enter text into fields or interact with buttons, but I'm having a difficult time wrapping my head around this.
I have a list of fields in Excel: First Name, Last Name, Email Address. There are over two thousand rows of information in the spreadsheet. The website I'm working with has a list of over three thousand rows of information (same information with the addition of a checkbox), except it only loads a maximum of 100 rows at a time. Every row of data in Excel exists in the web tool.
I'd like to have my procedure iterate over the names and check the corresponding row on the webpage. The problem is figuring out how to identify the input id for the corresponding checkbox. I'm not very familiar with HTML so my code is very rudimentary... I've spent much of the day writing this:
Sub Test()
Dim str As String
Dim ie As SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim tbls As Object
Dim tbl As Object
Dim trow As Object
Dim trs As Object
Dim Cell As Range
Set ie = New SHDocVw.InternetExplorer
ie.navigate "https://test.com"
ie.Visible = True
Set doc = ie.Document
Set tbls = doc.getElementsByTagName("TABLE")
For Each Cell In Range("A2:A2200")
For Each tbl In tbls
For Each trow In tbl.Rows
If InStr(1, trow.Cells(i).innerText, Cell.Offset(0, 1) & ", " & Cell) > 0 Then
Debug.Print trow.Cells(0).innerText
Stop
End If
Next trow
Next tbl
Next Cell
End Sub
The above code is able to identify the cell on the page that matches the cell in the workbook, but I can't figure out how to find the id to the associated checkbox. The site is private, so I can't provide a link. See below for what appears to be the relevant piece of source code for the site:
<div>
<table cellspacing="0" border="0" id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults" style="border-color:Black;border-width:1px;border-style:Solid;width:100%;border-collapse:collapse;">
<tr>
<td colspan="6">
<table id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_tabPager" border="0" cellpadding="5" width="100%">
<tr>
<td style="border-style:none;">
<a id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_lbPrev" disabled="disabled">Previous</a>
<a id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_lbNext" disabled="disabled">Next</a>
</td>
<td style="border-style:none;" align="right">
<b>Showing:</b>
<span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow"><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_0" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="10" checked="checked" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_0">10 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_1" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="25" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$1\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_1">25 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_2" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="50" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$2\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_2">50 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_3" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="100" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$3\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_3">100 People</label></span>
</td>
</tr>
<tr id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_trSelectOptions">
<td colspan="2" style="border-style: none;" align="left">
Select All on Page
Clear All on Page
<a class="link_w_img" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$ctl09", "", true, "", "", false, true))">
<img src="/images/all.gif" />
<span>Select Entire List</span></a>
<a class="link_w_img" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$ctl11", "", true, "", "", false, true))">
<img src="/images/none.gif" />
<span>Clear Entire List</span></a>
</td>
</tr>
</table>
</td>
</tr>
<tr class="gridheader" align="left">
<th scope="col">Select</th>
<th scope="col">Last Name</th>
<th scope="col">First Name</th>
<th scope="col">Middle Name</th>
<th scope="col">E-mail Address</th>
<th scope="col" style="width:0px;"> </th>
</tr>
<tr class="griditem" align="center">
<td>
<input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_SelectCheckBox" type="checkbox" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl03$SelectCheckBox" />
</td>
<td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Demo1</td>
<td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Test</td>
<td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();"> </td>
<td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Demo1#Test.com</td>
<td style="width:0px;">
<div id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip" class="popup">
<div class="popupbg">
<table>
<tr>
<td style="vertical-align:top;">
<span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_lblName" style="font-weight:bold;">Demo1, Test </span>
<br />E-mail:<span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_lblEmail">Demo1#Test.com</span><br />
<table cellspacing="0" border="0" id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_dgDemos" style="border-style:None;border-collapse:collapse;margin-top:-1.25em;">
<tr>
<td>
</td><td>
</td>
</tr><tr>
<td>
Employee ID:
</td><td>
N/A
</td>
</tr><tr>
<td>
Job Title:
</td><td>
N/A
</td>
</tr><tr>
<td>
Location:
</td><td>
N/A
</td>
</tr>
</table>
</td>
<td style="width:50px;vertical-align:top;">
<img src="/images/user_popup.jpg" border="0" width="48" height="48"/>
</td>
</tr>
</table>
</div>
</div>
</td>
In the above example, the Last Name is Demo1, the First Name is Test, and the Email is Demo1#Test.com, and the input id for the checkbox is ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_SelectCheckBox.
My question is, how do I identify the input id after I've identified table row? Or, perhaps that's impossible and I'm going about this the wrong way? Any thoughts will be much appreciated.
Edit
This is what I tried that gives me the Runtime error (Object variable or With block variable not set):
trow.Cells(0).getElementsByTagName("input")(0).Click
I also tried this:
set input = trow.Cells(0).getElementsByTagName("input")(0)
input.Click
And finally, both these produce the same error:
Debug.Print trow.Cells(0).getElementsByTagName("input")(0).innerText
Debug.Print trow.Cells(0).getElementsByTagName("input")(0)
This is the output of the Debug.Print in my original code:
Demo1, Test
E-mail: Demo1#Test.com
Employee ID: N/A
Job Title: N/A
Location: N/A
The only pieces I care about (in the output) are the first two lines, the email and name of the individual.
This test worked for me. Looks like the rows you want all have class="griditem", so you can check all table rows for that class name. When you find a match on that and on your cell values, you should be able to find the checkbox as shown.
Sub Tester()
Dim d As New HTMLDocument, trs, rw
d.body.innerHTML = _
"<section><table><tr class='griditem'><td>" & _
"<input type='checkbox' id='id_1'></td>" & _
"<td>Foo</td></tr><tr class='griditem'><td>" & _
"<input type='checkbox' id='id_2'></td>" & _
"<td>Bar</td></tr></table></section>"
Set trs = d.getElementsByTagName("tr")
For Each rw In trs
If rw.className = "griditem" Then
'check cell values for match....
Debug.Print rw.Cells(0).getElementsByTagName("input")(0).ID
End If
Next rw
End Sub
Give a try to this.
Set ElementCol = ie.Document.getElementsByTagName("input")
For Each btnInput In ElementCol
'your code here
Next btnInput
Using Selenium Webdriver for FF/IE using C# (.Net)
Below is my page source and I am trying to use the CssSelector to find/contains the particular name from my page and i have tried with the below code but resulting in error, any help?
//code
driver.FindElement(By.CssSelector("td:contains('John John')"))
//error:
e {"Unable to find element with css selector == td:contains('John John')"} System.Exception {OpenQA.Selenium.NoSuchElementException}
//my html code:
<div id="ctl00_ContentPlaceHolder1_AddeCardControl1_gv_ctl01_RecordCount" style="float:right; padding-right:10px; margin-top:3px;">
<b>308</b> Items Found
</div>
</td>
</tr>
<tr class="item">
<td align="center">Edit</td>
<td align="center" style="width:15px;"></td>
<td>John John</td>
<td> </td>
<td> </td>
<td> </td>
<td><img src="check.png" alt='Active' style='display: ;' /></td>
<td>9/7/2012 11:15:08 PM</td>
</tr>
<tr class="altItem">
<td align="center">Edit</td>
<td align="center" style="width:15px;"></td>
<td>John Schulz</td>
<td> </td>
<td>Visitors</td>
<td> </td>
<td><img src="check.png" alt='Active' style='display: ;' /></td>
<td>9/7/2012 6:28:29 PM</td>
</tr>
<tr class="item">
<td align="center">Edit</td>
<td align="center" style="width:15px;"></td>
<td>Parker Smith</td>
<td> </td>
<td>Visitors</td>
<td> </td>
<td><img src="check.png" alt='Active' style='display: ;' /></td>
<td>9/7/2012 6:01:28 PM</td>
</tr>
<tr class="altItem">
<td align="center">Edit</td>
<td align="center" style="width:15px;"></td>
<td>Test 123</td>
<td> </td>
<td>Visitors</td>
<td> </td>
<td><img src="check.png" alt='Active' style='display: ;' /></td>
<td>9/7/2012 1:36:45 PM</td>
</tr>
<tr class="item">
<td align="center">Edit</td>
<td align="center" style="width:15px;">
The :contains pseudoselector is not part of the W3C CSS Selector standard. As such, browsers do not support selecting elements using it. Some JavaScript CSS selector engines (Sizzle, the engine used by jQuery, for example) provide a :contains pseudoselector, but its presence cannot be relied on.
If you must find an element by the text contents of the element, your only solution at this point is to use XPath. A (very poorly performing) example of how to find this in your case would be as follows:
IWebElement element = driver.FindElement(By.XPath("//td[contains(., 'John John')"));
Note that a better solution will always be to have the application you're automating have proper IDs for the elements you need to find. You should be using text to find elements only as a last resort.
You can try this
var webElements = (Driver.FindElements(By.XPath(elementXpath))).ToList();
webElements.FindIndex(item => item.Text.Contains("John John").Click()
where "elementXpath" is path to each cell in table "names". So you get the list of names and then just find a match. You'll get your item clicked.
You may have better luck using the javascript executor to click the element. I am using a very slow IE9 64bit emulator and it seems the only way to click on certain buttons is to use the javascript executor.
CSS selectors aren't very useful here, because CSS selectors work on the html structure i.e. type, relationship and attributes of web elements; they don't work well on the html content, which in this case is the internal text content 'John John'.
But, xpath will work for this job. The function you need is text() which returns the element's inner text content:
//td[text()='John John']
So your webdriver code should look like this:
driver.FindElement(By.xpath("//td[text()='John John']"));
P.S. All locators verified using Firepath in firefox.
You can use the below code:
driver.FindElement(By.XPath("//td[contains(text(), 'John John')"));