Find Td with Specific Text, and Operate on the Td Right After That One

Find td with specific text, and operate on the td right after that one?

My initial answer missed the point about updating the text, here is a sample with this included:

$(".test td:contains(Heading 1)")
.next().text(function(){
return $(this).text() + " Hello"
});

I don't believe you can use the append method as I think it only works to add html tags.

Find td with specific text, and operate on the ALL td's on its right?

There are other kind of resources that will let you select elements on the DOM like nextAll() or siblings(), based on the code you already have just change that:

$(".test td:contains(Row 3)")
.nextAll().text(function(){
return $(this).text() + "xyz"
});

How to find all content in a td after a th with specific text?

Use the selector contains() to find all elements containing a given text. Combine with siblings to find the td-element. Combine them together:

$('th:contains("Date")') // Find th with text "Date" in it
.siblings('td') // Get sibling td
.find('.published') // Find something with class 'published'
.text().substring(0, 4) // Get first 4 charatecters of text

Proof of concept (enable console): https://jsfiddle.net/petervanderwal/xn73d0d2/1/

Find first TD with text in a TR with JQuery

Here's a simpler, more elegant solution:

$('tr').find('td:not(:empty):first').css('background', 'red');​

Fiddle: http://jsfiddle.net/dandv/JRcEf/

It just says in jQuery what you mean: "target the first td in each tr that is not empty".

jQuery: Get last 'td' containing some text without using each

This should work now

var td = $('table td:last');

function findTD() {
if (!td.text().replace(/\u00a0/g, "").length) {
if (td.parent().is(':not(:first)')) {
td = td.parent().prev().find('td');
return findTD();
} else {
return 'No text found!';
}
} else {
return td.text();
}
}

alert(findTD());

FIDDLE

How to get the second element by matching text from the first element. In Selenium, Python

You can nested one [ ] inside another [ ]

Maybe first use TITLE to get tr - //tr[td[text()="TITLE"]]

And later get second td in this tr - (//tr[td[text()="TITLE"]]//td)[2]


EDIT:

You can also use following-sibling

//td[text()="TITLE"]/following-sibling::td

Full working example:

from selenium import webdriver
from selenium.webdriver.common.by import By
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager

#driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

html = '''<!DOCTYPE html>
<html>
<body>
<table>
<tr></tr>
<tr>
<td class="gridTD">TITLE</td>
<td class="gridTD">NAME</td>
</tr>
<tr></tr>
</table>
</body>
</html>
'''

driver.get("data:text/html;charset=utf-8," + html)

item = driver.find_element(By.XPATH, '(//tr[td[text()="TITLE"]]//td)[2]')
print(item.text)

item = driver.find_element(By.XPATH, '//td[text()="TITLE"]/following-sibling::td')
print(item.text)


Related Topics



Leave a reply



Submit