JSoup extracting values from <td> tag - java

I'm trying to extract values from td tag, but it doesn't work, one of td contains value which is called "Technology", and I want to get td value which is after him, for example td is 'Technology' and td which is after that: 'Windows Server' <- how can I get this value? here is a code:
</td>
</tr>
</tbody>
</table>
</dd>
<dt>
Informacje o kursie</dt><dd id="course-info"><table><tbody>
<tr>
<td>Odbiorcy:</td>
<td style="text-transform: capitalize;">IT Professionals</td>
</tr>
<tr>
<td>Technologia:</td><td>Windows Server</td>
</tr>
<tr>
<td>Poziom:</td><td>300</td>
</tr>
<tr>
<td>Bieżąca wersja:</td><td>B</td>
</tr>
when I'm doing something like that:
for (Element element : doc.select("#course-info").first().children()){
if(element.text().contains("Tech")){
System.out.println(element.nextElementSibling().html());
}
}
Then NPE appears (element), but when I delete this loop, whole method works fine.

The specific CSS selector for "Windows Server" is #course-info > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2).
If you want to know which selector to use, open your browser's developers tools (F12), and inspect the DOM. Select the desired element and get the selector.
I believe that you have more rows in your table, so you'll have to loop thru. the table, by changing one (or more) of the number constants in the above selector.

Related

Selenium WebDriver Java / JUnit - Getting table element

I have the following table:
<table>
<tbody>
<tr>
<td>
</td>
<td>
User1
</td>
</tr>
<tr>
<td>
</td>
<td>
User2
</td>
</tr>
</tbody>
</table>
I want to find the a-tag of the tr, where the data User2 is in the same row. I know that I can find an a-tag with partial link like findElement(By.partialLinkText("/ResetPassword/")); (the number 2 can change, so I can´t use it as seperator). But I need to seperate it by User. Is there a solution like tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/"));?
This XPath should do the trick for you. .//tr[td[normalize-space(text())='User2']]//a Just keep changing "User2" part with the desired user value.
Hi I was wondering if you could use the .getValue() statement with /#a at the end of the xpath to locate the attribute "a".
Main thing to do is find a bulletproof xpath to locate the User2 row once you have done that finding the value of "a" should be easy enough.
I hope this helps
U can try something like this(not sure though)-
List<WebElement> list=table.findElements(By.tagName("tr"));
List<WebElement> tdvalues=null;
for(WebElement web:list){
tdvalues=web.findElements(By.tagName("td"));
if(tdvalues.contains("User2")){
System.out.println(tdvalues.get(0).getText());//0th position contains the link
}
tdvalues.clear();
}

webdrivr click the button which will call Javascript can't work

(1) html:
<table class="title_button" onclick="onSave();" onmouseout="this.className='title_button'; save_moveout();" onmouseover="this.className='title_button_hi'; save_movein();" title="Copy Running-config to Startup-config">
<tbody>
<tr>
<td>
<img id="title_btn_save" src="https://192.168.100.116/image/title_btn_alert.gif">
</td>
<td id="title_save">Save</td>
</tr>
</tbody>
</table>
(2) Webdriver Java:
tried these two ways both can't work.
driver.findElement(By.id("title_save")).click();
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td/table/tbody/tr/td[8]/table/tbody/tr/td/div/table/tbody/tr/td[2][#id='title_save'")).click();
(3) Exception:
webdriver element is not currently interactable and may not be manipulated.
You are trying to click the td element, but the actual "clickable" element here is the table:
driver.findElement(By.className("title_button")).click();
It looks like element you try to click in disabled and click operation cannot be performed to this element.
Try to perform click operation on another element e.g. on :
driver.findElement(By.css(".title_button tr")).click();

How to select radio button in selenium webdriver ? dynamic select of radio button

<tbody>
<tr class="odd">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4493B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4493B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:dece:cs</td>
</tr>
<tr class="even">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4494B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4494B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:coord:cs</td>
</tr>
<tr class="odd">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4495B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4495B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:300</td>
</tr>
<tr class="even">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4495B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4495B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:10</td>
</tr>
</tbody>
</table>
i want to select the radio button corresponding to urn:dece:org:org:dece:10 which is fourth row in the html provided. the row may change sometimes based on some inputs in AUT. please provide me the way to select it.
thanks in advance
Since the text never changes, you can use that as a starting point within the DOM, and use XPath to navigate through to the input you need:
//td[.='urn:dece:org:org:dece:10']/parent::tr/descendant::input[#id='nodeAccountOid' and #type='radio']
Get the td that has it's text equal to urn:dece:org:org:dece:10
Get that td's parent tr
From that parent tr, get the input that has an id equal to nodeAccountOid and has a type of radio.
Therefore it doesn't matter where exactly the elements are, as long as the XPath locator can navigate up to the parent and back down again to the input you need.
The problem you are facing is to locate a certain element in the web page that may occur in different positions.
If there is another element which can be identified easily by its content, id or name and there is a static relation between this element and the one you would like to locate, then you could use xpath. There are also some examples for this in the selenium documentation as far as I know.

Selenium read a table and write into a file line by line

I am looking for the help to read a table span using selenium code and write span into the file,
following is my html code
<table>
<tbody><tr>
<tr>
<td><span>
FIRST
</span>
</td>
</tr>
<tr>
<td>
<span>SECOND</span>
</td>
</tr>
<tr>
<td>
<span>THIRD</span>
</td>
</tr>
<tbody>
</table>
I need to write FIRST SECOND THIRD on a file in java.
Thanks a lot.
I suppose you have located/found the table WebElement. Then you can get span elements content like this:
List<WebElement> spanElements = tableElement.findElements(By.ByTagName("span"));
for (WebElement element : spanElements) {
String spanContent = element.getText();
//save it to a collection or a StringBuilder, then write it to a file
}
Having a look at this and this might help.
1) XPath that gets all text is:
//span/text()
2) in java code you may type something like
String text = selenium.getText("xpath=//span");

How to iterate over <td> tags with condition using jsoup

I am able get all text with in tags but I want to access only specific td tags.
Eg.I want to get data of second cell text whose first cell html contains attribute
a name="manufacturer"
or Content.I am using Jsoup.
<tabel>
<tr>
<td><a name="Manufacturer"></a>manufacturer</td>
<td>happiness</td>
</tr>
<td>manuf</td>
<td>hap</td>
</tr>
<tr>
<td>tents</td>
<td>acd</td>
</tr>
<tr>
<td><a name="Content"></a>Contents</td>
<td>abcd</td>
</tr>
</tabel>
I am using the code ..
doc.select("a[name=Manufacturer]");
..but its giving me the reference of cell one ,I need to go to cell two get cell two text
You need to use selector like [attr=value]: elements with attribute value, e.g. [width=500].
Take a look at official documentation Selector Syntax

Categories