[英]how can i locate element and get the number by use selenium python
I am trying to use selenium to locate this element and get the number this element, but I need selenium to get the number 8864.00 How can I locate this element and get thenumber我正在尝试使用 selenium 来定位此元素并获取此元素的编号,但我需要 selenium 来获取编号 8864.00 如何定位此元素并获取编号
HTML: HTML:
<div class="hidden-xs" style="position:fixed;z-index:2456;top:1em;right:0em;background-color:#fff;">
<table class="table cart table-hover table_price" id="current_price">
<thead>
<tr>
<th colspan="2">Bid</th>
<th colspan="2">Ask</th>
</tr>
<tr>
<th><div style="width:35px;">Size</div></th>
<th>Price</th>
<th>Price</th>
<th><div style="width:35px;">Size</div></th>
</tr>
</thead>
<tbody>
<tr><td class="price_bid">334</td><td class="price_bid">8,858.00</td><td class="price_ask">8,870.00</td><td class="price_ask">210</td></tr>
<tr><td class="price_bid">286</td><td class="price_bid">8,858.50</td><td class="price_ask">8,869.50</td><td class="price_ask">8,832</td></tr>
<tr><td class="price_bid">287</td><td class="price_bid">8,859.00</td><td class="price_ask">8,868.50</td><td class="price_ask">490</td></tr>
<tr><td class="price_bid">315</td><td class="price_bid">8,860.00</td><td class="price_ask">8,868.00</td><td class="price_ask">367</td></tr>
<tr><td class="price_bid">1</td><td class="price_bid">8,860.50</td><td class="price_ask">8,867.50</td><td class="price_ask">7,044</td></tr>
<tr><td class="price_bid">5</td><td class="price_bid">8,862.00</td><td class="price_ask">8,867.00</td><td class="price_ask">400</td></tr>
<tr><td class="price_bid">10</td><td class="price_bid">8,862.50</td><td class="price_ask">8,866.50</td><td class="price_ask">3,253</td></tr>
<tr><td class="price_bid">286</td><td class="price_bid">8,863.00</td><td class="price_ask">8,866.00</td><td class="price_ask">56</td></tr>
<tr><td class="price_bid">286</td><td class="price_bid">8,863.50</td><td class="price_ask">8,865.50</td><td class="price_ask">400</td></tr>
<tr><td class="price_bid">634</td><td class="price_bid">8,864.00</td><td class="price_ask">8,864.50</td><td class="price_ask">921</td></tr>
</tbody>
</table>
</div>
could you help me again i need last of td element你能再帮我一次吗?我需要最后一个 td 元素
From what I understand, you are looking to get the "bid price" from the last row in the table .据我了解,您希望从表格的最后一行获得“投标价格” 。
There are a few possible solutions here:这里有几个可能的解决方案:
XPath and its last()
function : XPath 及其
last()
function :
last_row = driver.find_element_by_xpath("//table[@id = 'current_price']/tbody/tr[last()]") size, price_bid = last_row.find_elements_by_class_name("price_bid") print(price_bid.text)
Note that as there are 2 elements with class price_bid
in each row, we are getting both and unpack into size
and price_bid
variables here请注意,由于每行中有 2 个元素带有 class
price_bid
,因此我们在此处获取两者并解压缩为size
和price_bid
变量
getting all elements matching a specific selector and getting the last one with Python:获取与特定选择器匹配的所有元素并使用 Python 获取最后一个元素:
last_price_bid = driver.find_elements_by_css_selector("table#current_price tr td.price_bid")[-1].text print(last_price_bid.text)
This is though, generally speaking, a waste of resources and have poor performance as we are instantiating lots of WebElement
instances but using a single one.但是,一般来说,这会浪费资源并且性能很差,因为我们实例化了许多
WebElement
实例但使用了一个实例。
basically, any combination of the above.基本上,以上任意组合。 For example, you can get directly to the last bid price with:
例如,您可以通过以下方式直接获得最后的出价:
last_price_bid = driver.find_element_by_xpath("//table[@id = 'current_price']/tbody/tr[last()]/td[2]") print(last_price_bid.text)
convert the whole table into a Python list of bid and ask prices:将整个表格转换为 Python 买卖价格列表:
prices = [] for row in driver.find_elements_by_css_selector("table#current_price tbody tr"): size_bid, price_bid, price_ask, size_ask = row.find_elements_by_tag_name("td") prices.append({ "size_bid": size_bid.text, "price_bid": price_bid.text, "size_ask": size_ask.text, "price_ask": price_ask.text }) print(prices[-1]["price_bid"])
This option gives more flexibility to query the prices table in case you need to process values from other rows as well.如果您还需要处理来自其他行的值,此选项为查询价格表提供了更大的灵活性。
you could even get the raw HTML of the table (with eg .get_attribute("outerHTML")
) and put it into a pandas DataFrame via read_html()
您甚至可以获得表的原始 HTML (例如
.get_attribute("outerHTML")
)并将其放入 pandas DataFrame 通过read_html()
As you can see, there is a number of different ways to parse out this table.如您所见,有许多不同的方法可以解析出这张表。 Hope the options above give you enough material to figure out the best approach.
希望以上选项为您提供足够的材料来找出最佳方法。
You can use this:你可以使用这个:
text = driver.find_element_by_xpath("//td[@class='price_bid']").text
print(text)
To get text from the td
element.从
td
元素中获取文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.