我用的python
,selenium
包等按我的需要。我正在创建GUI(图形用户界面),它将显示通过的总人数。我们已经拥有的硬件直接到特定的网站报告这是写在php
和数据库sql
和mariadb.
<span class="info-box-number f28 fc-666" id="tot_count">11</span>
这是代码示例,我要从中捕获“ 11”。我尝试使用'id','class'和'xpath'来捕获它,.text
但到目前为止我找不到解决方案。我提到了其他一些问题,但这并没有帮助我。
注意:我要捕获的所有类和ID都是唯一的。
如果有人有更好的主意,我愿意提出建议,您的建议将不胜感激。
要打印文本 11
,可以使用以下两种定位策略之一:
使用class_name
和get_attribute("textContent")
:
print(driver.find_element_by_class_name("info-box-number").get_attribute("textContent"))
使用css_selector
和get_attribute("innerHTML")
:
print(driver.find_element_by_css_selector("span.info-box-number#tot_count").get_attribute("innerHTML"))
使用xpath
和文字属性:
print(driver.find_element_by_xpath("//span[contains(., 'info-box-number') and @id='tot_count']").text)
理想情况下,您需要为引入WebDriverWait,visibility_of_element_located()
并且可以使用以下两种定位策略之一:
使用CLASS_NAME
和get_attribute("textContent")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "info-box-number"))).get_attribute("textContent"))
使用CSS_SELECTOR
和文字属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.info-box-number#tot_count"))).text)
使用XPATH
和get_attribute()
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'info-box-number') and @id='tot_count']"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在如何使用Selenium检索WebElement的文本中找到相关的讨论-Python
链接到有用的文档:
get_attribute()
方法 Gets the given attribute or property of the element.
text
属性返回 The text of the element.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句