简体   繁体   English

如何使用 Selenium 和 Python 获取网页中的结果数量?

[英]How do I get the number of results in a webpage with Selenium and Python?

I am new to python and Selenium and I am trying to automatize a process that is copying a search query from excel, introducing it in a specific textbox and look for number of results under the query.我是 python 和 Selenium 的新手,我正在尝试自动化一个从 excel 复制搜索查询的过程,将它引入特定的文本框中并在查询下查找结果数量。 Once I have the number of results, I want to copy this number in excel again.一旦我得到结果的数量,我想再次将这个数字复制到 excel 中。 I could build almost all the code but I don't know how to get the number of results given by my search.我可以构建几乎所有的代码,但我不知道如何获得我的搜索给出的结果数量。 In other words, I would like to extract the number 15 (I would also be happy with the whole text 'Headlines 1 - 15')which is contained in the following line of html code:换句话说,我想提取包含在以下 html 代码行中的数字 15(我也会对整个文本“标题 1 - 15”感到满意):

 <span class="resultsBar" data-hits="15">Headlines 1 - 15 of 15</span>

I tryed many different things, ie:我尝试了很多不同的东西,即:

 driver.find_element_by_xpath("/html/body/form[2]/div[2]/div[2]/div[5]/div[2]/div[3]/div/div[1]/div/div[1]/table/tbody/tr/td/span[2]").getattribute("data-hits") driver.find_element_by_xpath("/html/body/form[2]/div[2]/div[2]/div[5]/div[2]/div[3]/div/div[1]/div/div[1]/table/tbody/tr/td/span[2]").getattribute("value") driver.find_element_by_xpath("/html/body/form[2]/div[2]/div[2]/div[5]/div[2]/div[3]/div/div[1]/div/div[1]/table/tbody/tr/td/span[2]").getText()

but none of this worked.但这些都没有奏效。 Do you have any suggestion?你有什么建议吗? Thanks in advance, S.提前致谢,S。

使用get_attribute("data-hits")并遵循xpath

driver.find_element_by_xpath("//span[@class='resultsBar']").get_attribute("data-hits")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM