在每个元素匹配特定 class 名称后获取下一个元素
Get next element with after each element matching a specific class name
设置
我有以下 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>
我只需要 class 名称 "order-activ_order_delivered"
和 "tracking-url tracking-url-activ_order_delivered"
.
行中的信息
此外,任何"order-activ_order_delivered"
行的信息都与随后的"tracking-url tracking-url-activ_order_delivered"
行有关。
到目前为止的代码
下面的循环获取每个 "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
问题
要获取我尝试的后续 "tracking-url tracking-url-activ_order_delivered"
行的信息,
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")
但这给出了 InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=100.0.4896.127)
要么是因为您不能在 CSS_SELECTOR
上使用 following-sibling
,要么是因为其他原因。
如何从后续 "tracking-url tracking-url-activ_order_delivered"
行中获取?
following-sibling
不是 CSS_Selector
语法,它是 xpath
语法。
您的代码应该像识别特定元素然后使用 xpath
检查 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']")
或
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]")
如果想使用 css selector
可以使用这个 return 所有 tracking-url.
tr[class^='order-activ_order_delivered']+tr.tracking-url.tracking-url-activ_order_delivered
代码:
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
设置
我有以下 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>
我只需要 class 名称 "order-activ_order_delivered"
和 "tracking-url tracking-url-activ_order_delivered"
.
此外,任何"order-activ_order_delivered"
行的信息都与随后的"tracking-url tracking-url-activ_order_delivered"
行有关。
到目前为止的代码
下面的循环获取每个 "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
问题
要获取我尝试的后续 "tracking-url tracking-url-activ_order_delivered"
行的信息,
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")
但这给出了 InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=100.0.4896.127)
要么是因为您不能在 CSS_SELECTOR
上使用 following-sibling
,要么是因为其他原因。
如何从后续 "tracking-url tracking-url-activ_order_delivered"
行中获取?
following-sibling
不是 CSS_Selector
语法,它是 xpath
语法。
您的代码应该像识别特定元素然后使用 xpath
检查 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']")
或
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]")
如果想使用 css selector
可以使用这个 return 所有 tracking-url.
tr[class^='order-activ_order_delivered']+tr.tracking-url.tracking-url-activ_order_delivered
代码:
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