Selenium XPath multiple selection - selenium

I want to find all elements in the table that match the following conditions:
-div text contains '2019';
-div class='excellent';
here is the HTML code excerpt:
<tr>
<td>Name of Person1</td>
<td>
<div class="testDate">21/12/2019</div>
<div class="excellent"></div>
</td>
</tr>
<tr>
<td>Name of Person2</td>
<td>
<div class="testDate">01/12/2017</div>
</td>
</tr>
I tried this solution:
//tr/td[2][div/text()='21/12/2019'][div[#class='starred']]
but I need the year only and not entirely date.

Use the below xpath.
//tr/td[div[contains(.,'2019')]and div[#class='excellent']]
Screenshot:

Related

skrape.it process rows of a table by itself

I am new to the skrape.it library and try to extract content of a table.
Is there a way to process every table by it self, so I get a List with all texts of a row in a List with all rows? e.g.: List<List>
My current approach is to get all texts from every td and count all tr and then process these two lists by calculating the index of the required text.
One row looks like this:
<tr role="row">
<td class="grad" style="background:#067A25; width:50px">
<div class="grad_outer">
<span>4c</span>
</div>
</td>
<td class="name">
King Louie
</td>
<td>
Dave
</td>
<td>
11.03.2022
</td>
<td>
12
</td>
<td>
64.2
</td>
</tr>
I also need to extract the background css attribute of each first td tag. How can I do this?

XPATH to use preceding and following sibling in a single statement

I would like to scrape name, address informations between tag contains defendent text and another tag,
My HTML structure is:
<hr>
<H5>Defendant/Respondent Information</H5>
<span class="InfoChargeStatement">(Each Defendant/Respondent is displayed below)</span>
<table>
<tr>
<td><span class="FirstColumnPrompt">Party Type:</span></td><td><span class="Value">Defendant</span><span class="Prompt">Party No.:</span><span class="Value">1</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Name 1</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">Addr 1</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">city1</span><span class="Prompt">State:</span><span class="Value">aa</span><span class="Prompt">Zip Code:</span><span class="Value">Zip1</span></td>
</tr>
</table>
<hr>
<table>
<tr>
<td><span class="FirstColumnPrompt">Party Type:</span></td><td><span class="Value">Defendant</span><span class="Prompt">Party No.:</span><span class="Value">2</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Name 2</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">Addr2</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">City2</span><span class="Prompt">State:</span><span class="Value">st2</span><span class="Prompt">Zip Code:</span><span class="Value">zip2</span></td>
</tr>
</table>
<hr>
<H5>Related Persons Information</H5>
<span class="InfoChargeStatement">(Each Related person is displayed below)</span>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Unwanted Name</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">un addr</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">Unwanted City</span><span class="Prompt">State:</span><span class="Value">Unwanted city</span><span class="Prompt">Zip Code:</span><span class="Value">12345</span></td>
</tr>
</table>
<table></table>
<hr>
My current XPATH capturing the first occurence of Name and address properly, but if need to extract the multiple occurences, it also scrape the information from the unwanted h5 tags.
My current XPATH is,
"//*[contains(text(),'Defendant')]//following-sibling::table//span[text()='Name:' or text()='Business or Organization Name:']/ancestor-or-self::td/following-sibling::td//text()")
I tried including preceding sibling and following sibling but nothing gives my expected output,
My current output is..
names - [
Name1,
Name2
Unwanted Name,
]
Expected output is,
[
Name1
Name2
]
Kindly help.
try this:
"//H5[contains(text(),'Defendant')]/following-sibling::table[not(preceding-sibling::H5[not(contains(text(),'Defendant'))])]/tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"
It first selects the table that has not a preceding-sibling::h5 with text() that not contains 'Defendant' and than
selects from the correct table the tr where the first td meets your requirements and selects the second td
No need for double slashes which is bad for performance
EDIT 1
Since there are more preceding-sibling::h5 than the example shows, this XPath will deal with that:
"//H5[contains(text(),'Defendant')]/following-sibling::table[preceding-sibling::H5[1][contains(text(),'Defendant')]]//tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"
This will only select those tables that have as there first preceding-sibling::h5 the same h5 as we were interested in
EDIT 2
Actually now the first h5 select is redundant. This XPath will do:
"//table[preceding-sibling::H5[1][contains(text(),'Defendant')]]//tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"

iterate with v-for and data-attribute

I have a vuejs-datatable, and now I want to have an option-column with edit- / delete-links.
This is the table-body which gets iterated from the function getRows():
<tbody>
<tr v-for="(row, idr) in get_rows()" v-bind:key="idr">
<td>{{row.id}}</td>
<td>{{row.email}}</td>
<td>
<b-icon-pencil-square></b-icon-pencil-square>
<b-icon-trash></b-icon-trash>
</td>
</tr>
</tbody>
Now the td with the {{row.id}} and {{row.email}} are fine. However the :data-id="row.id" displays only the id of the first entry. Links in every row in my table have the same data-id. I do not understand why this is happening and what am I doing wrong.
Use code below (notice, it's not using data-id):
<tbody>
<tr v-for="(row, idr) in get_rows()" v-bind:key="idr">
<td>{{row.id}}</td>
<td>{{row.email}}</td>
<td>
<b-icon-pencil-square></b-icon-pencil-square>
<b-icon-trash></b-icon-trash>
</td>
</tr>
</tbody>

How can I reach the link inside TR using XPATH

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)

Unable to find element with css selector

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')"));