Use Xpath to Get Row Number - html

[View of the table and first row] Ultimately, I need to click on an edit button in td1 within a tr that is dynamic.
My plan was to find that tr[#] based on the text in td2 (the email address that is the identifier).
//table/tr/td[2][contains(text(),'me#address.com')]
[The HTML code] 2 Correctly highlights td2 of the row I need to capture. I'd like to get that tr# and then use the next line to click the element in tr[#]/td1, but I am stuck.

You can use the 'preceding-sibling' to select the desired element
//table/tr/td[2][contains(text(),'me#address.com')]/preceding-sibling::td
This will look for your element and then select the preceding td containing your desired link
Also it is worth noting that the '(text(),'me#address.com')' attribute you are using is case sensitive

Related

Extract a single row from a table

I’m trying to extract a single row from a table.
I'm using google sheet to create the links and in cell D3 it contains this url.
https://www.wsj.com/market-data/quotes/AAPL/options
I have several links in cell D3 to go through.
The word "Last Trade" appears several times in different tables but I'M ONLY INTERESTED IN THE VERY FIRST TABLE FROM THE TOP.
with this word and once this word is found i'm looking to extract the ROW just above it.
Below is the IMPORTXML, and its needs modification and it should be able to pull that last row.
=IMPORTXML(D3,"//tr[td1/#class='acenter inthemoney'][last()]")
Any help would be greatly appreciated.
Thanks.
For that row you will need:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]
And then pick the wright td...it's unclear which td you want. So if you wanted the third td the XPath would be:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[3]
Its always the first table that ends with the word LAST TRADE and the row above it that i'm looking to extract, so in this case this is the row that i'm looking to extract, below is the picture.
https://www.wsj.com/market-data/quotes/AAPL/options
In the above case where you want the first td the XPath will then be
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[1]

Is there a way to access the first element in a column on a website using VBA?

Here is a screenshot of a column in a website page.
It is located in that way in the website page :
As you can see, all the rows have a 'Completed' button you can pres and followed by a number of lines. These rows refer to exports. So the columnis not static and is constantly changing.
However, everytime i run the macro i want to access the first row of the column.
Here is a sample code of he HTML code of the first 'Completed' button in the screenshot above:
I have many that have the same class name. Look at the highlighted rows as an example in the picture below:
I really have no idea how to write a VBA code to always access the first 'Completed' bytton in this column.
PS: In the HTML code, in the tag "a", the onclick="....." is constantly changing. So i cannot use this as an argument to access the desired field and click on the desired button.
Please if anyone could help me figure out how to do this, i would really be happy.
Thank you :)
If you want to click the 'Completed' button in the first column, you can use the code below:
Set doc = objIE.Document
doc.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
The code get the first <tr> then get the first <td> then get <a> in it.
<tr> tags are rows, <td> tags are cells inside those rows. You did not provide enough code to show the entire table, but generally speaking to access the first row of a table, you would need to refer to the collection object and use the index number you want.
.getElementsByTagName("tr")(0)
This will refer to the first row of a table. Same with getting the first column in the first row of your table:
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0)
Once you tracked down the particular cell, now you are wanting to click the link. You can use the same method as above.
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
And a final note, the first row of a table could be a header, so you may actually want the 2nd row (1) instead.
Thanks for updating with more HTML code. I am going to slightly switch gears and use querySelector() to grab the main table.
doc.querySelector("#divPage > table.advancedSearch_table > tbody"). _
getElementsByTagName("tr")(3).getElementsByTagName("td")(3).Children(0).Click
See if this works for you.

Extracting Text from a HTML Table with no Identifier/Name via Selenium

I'm trying to figure out how to extract the results generated from this site:
mailtester.com when entering a full email. Take the email address laura.singer#pfizer.com for example. After entering this address and clicking submit, a HTML table thats highlighted green will show up. I want to get the text "Email address is valid" which is the 5th child of the 5th row of this table it seems. However, there are no ids or names, and there are two tables, so its a bit tricky.
This is what I have right now:
wait = WebDriverWait(self.browser, 300)
inputEmail=wait.until(EC.presence_of_element_located((By.NAME, "email")))
inputEmail.clear()
inputEmail.send_keys(email)
inputEmail.submit()
somethin = self.browser.find_element_by_xpath("//table")
somethin = somethin.text
print(somethin)
It prints "Email address" which is incorrect. Looks like there are two tables which do not have ids or names.
Try this CSS selector. It basically means find the TABLE tag that is a child (>) of an element with an ID (#) content
somethin = self.browser.find_element_by_css_selector("#content > table")

Selenium: How to get text from within an html tag which has another tag in it

<a class="spf-link current" href="/orders/returns?offset=0&limit=25">
<span class="pg-helpText">Page</span>
1
</a>
I need to read the value '1' in my test. When I do a get text using css-selector .spf-link.current, it gets me "Page 1". I only need '1'. How do I exclude the text from the span tag.
You will need Javascript childNodes to get the text from the nodes.
The childNodes property returns a collection of a node's child nodes,
as a NodeList object.
The nodes in the collection are sorted as they appear in the source
code and can be accessed by index numbers. The index starts at 0.
WebElement element = driver.findElement(By
.cssSelector(".spf-link.current"));
String node_text=(String)((JavascriptExecutor)driver)
.executeScript("return arguments[0].childNodes[2].nodeValue",element);
This should give you the value 1 as node_text when you run the JavascriptExecutor. Other way is to split the text Page 1 on the basis of space. But, i would prefer childNodes.
Let me know if you have any queries.
Try the following:
//span[#class="pg-helpText" and contains(text(), 'Page')]/following-sibling::span/span
It is not normal to customize untagged text between tags.
I recommend you to change the value to your desired result:
<a class="spf-link current" href="/orders/returns?offset=0&limit=25">1</a>
...and when you want to show it in other place, do it as var ThePage = "Page " + [the page number]
Other solution is to include it into another tag as a div or as another span with a different class or id.

Select a row based on the contents of a cell with xpath

I have a table that consists of multiple rows that each contain 5 cells, like this:
<tr>
<td></td>
<td>123456</td>
<td>statusText</td>
<td><a>linkText</a></td>
<td>editButton</td>
</tr>
The 123456 could be any string of random letters and numbers. I want to be able to select a link based on the contents of the second cell in the table. I've been trying something like this:
//tr[contains(td, '123456')]
to get me to the cell, but it either returns every row or nothing, depending on how I tweak the xpath.
I've been trying something like this:
//tr[contains(td, '123456')]
to get me to the cell, but it either
returns every row or nothing,
depending on how I tweak the xpath
You get what you asked for. The above XPath expression selects any tr element (row) in the document that has (at least one) td child whose string value contains '123456'.
But you want:
//tr/td[text() = '123456']
this selects every td element (cell) in the document, that has a text node child, whose string value is '123456'.
There can be different variations, depending on whether a td may have more than one text nodes and on whether the white space in a text node should be normalized, but the question doesn't provide any information if any of these apply in this particular case.
I'd research something like //tr[string(td[2]) = '123456']. If this does not work, I'd look up XPath axes.