'Get next element with after each element matching a specific class name
Set-up
I have the following html table,
<table>
<tbody>
<tr class="order-activ_order_delivered">
<tr class="tracking-url tracking-url-activ_order_delivered">
<tr class="order-activ_order_delivered">
<tr class="tracking-url tracking-url-activ_order_delivered">
<tr class="order-activ_order_delivered">
<tr class="tracking-url tracking-url-activ_order_delivered">
<tr class="order-complete">
<tr class="empty-tracking-url order-complete">
<tr class="order-activ_order_delivered">
<tr class="tracking-url tracking-url-activ_order_delivered">
<tr class="order-activ_order_delivered">
<tr class="tracking-url tracking-url-activ_order_delivered">
<tr class="order-complete">
<tr class="empty-tracking-url order-complete">
<tbody>
</table>
I need ONLY the information in the rows with class names "order-activ_order_delivered" and "tracking-url tracking-url-activ_order_delivered".
Moreover, the information of any "order-activ_order_delivered" row is related with the subsequent "tracking-url tracking-url-activ_order_delivered" row.
Code so far
The following loop gets each "order-activ_order_delivered",
order_table = el_css(browser,'#my-orders-table > tbody')
for order in order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']"):
#code to get data from row
Question
To get the information of the subsequent "tracking-url tracking-url-activ_order_delivered" row I tried,
for order in order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']"):
order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']/following-sibling::tr")
but this gives an InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=100.0.4896.127)
Either because you can't use following-sibling on a CSS_SELECTOR or because something else.
How do I get the from the subsequent "tracking-url tracking-url-activ_order_delivered" row?
Solution 1:[1]
following-sibling is not CSS_Selector syntax it is xpath syntax.
Your code should be like identify the specific element and then using xpath to check the following-sibling
for order in order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']"):
nextelement=order.find_element(By.XPATH, "./following-sibling::tr[@class='tracking-url tracking-url-activ_order_delivered']")
Or
for order in order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']"):
nextelement=order.find_element(By.XPATH, "./following-sibling::tr[1]")
In case of want to use css selector use can use this which will return all tracking-url.
tr[class^='order-activ_order_delivered']+tr.tracking-url.tracking-url-activ_order_delivered
Code:
for order in order_table.find_elements(By.CSS_SELECTOR,
"tr[class^='order-activ_order_delivered']+tr.tracking-url.tracking-url-activ_order_delivered"):
nextelement=order
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
