Python 硒打印文本
Python Selenium print text
页面上有这样的 HTML 代码,我需要获取其文本进行检查,或者更确切地说 740 使用 Selenium。
<button data-v-3630a784="" class="currency-btn mr10"><div data-v-3630a784="" class="progress"><div data-v-3630a784="" class="progress-bg anim" style="transform: scaleX(0.458333);"></div></div> <div data-v-3630a784="" class="num flex align-center icon-mana">740</div> <!----></button>
要打印文本 740 您可以使用以下任一方法 :
使用css_selector和get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//button[contains(@class, 'currency-btn')]//div[@class='num flex align-center icon-mana']").text)
要提取文本 740 理想情况下,您需要引入 WebDriverWait for the and you can use either of the following :
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana"))).text)
使用 XPATH 和 get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(@class, 'currency-btn')]//div[@class='num flex align-center icon-mana']"))).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
You can find a relevant discussion in
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium
页面上有这样的 HTML 代码,我需要获取其文本进行检查,或者更确切地说 740 使用 Selenium。
<button data-v-3630a784="" class="currency-btn mr10"><div data-v-3630a784="" class="progress"><div data-v-3630a784="" class="progress-bg anim" style="transform: scaleX(0.458333);"></div></div> <div data-v-3630a784="" class="num flex align-center icon-mana">740</div> <!----></button>
要打印文本 740 您可以使用以下任一方法
使用css_selector和
get_attribute("innerHTML")
:print(driver.find_element(By.CSS_SELECTOR, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//button[contains(@class, 'currency-btn')]//div[@class='num flex align-center icon-mana']").text)
要提取文本 740 理想情况下,您需要引入 WebDriverWait for the
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana"))).text)
使用 XPATH 和
get_attribute("innerHTML")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(@class, 'currency-btn')]//div[@class='num flex align-center icon-mana']"))).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
You can find a relevant discussion in
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium