无法获取div的文本

2024-09-30 05:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

<div _ngcontent-tpr-c123="" class="panel panel--bordered">hac-action: 1: /wae:wae/wae-ha:ha-config/ha-data-sync</div>
old_config_xpath = "//div[contains(text(),'hac-action')]"
element_value = wae_base_params[self.driver_key].find_element_by_xpath(old_config_xpath).text

元素值为空

注意:上面的HTML代码不是网页上可见的内容


Tags: textdivconfigactionelementxpatholdclass
2条回答

我没有看到该网站的页面源代码,但请测试以下代码,并让我知道结果。在过去,我使用get_属性('innerHTML')而不是'text',它工作正常。如果你能告诉我结果,我会很高兴的

old_config_xpath = "//div[contains(text(),'hac-action')]"
element_value = wae_base_params[self.driver_key].find_element_by_xpath(old_config_xpath).get_attribute('innerHTML')

任何元素的位置都是在DOM上

确保元素位于DOM上的方法是使用带有预期_条件的^{}

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(browser, 10)

element_value = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(text(),'hac-action')]]"))).text

相关问题 更多 >

    热门问题