Javascript Django selenium如何使用find_元素获取页面文本

Javascript Django selenium如何使用find_元素获取页面文本,javascript,python,django,selenium,django-testing,Javascript,Python,Django,Selenium,Django Testing,我正在尝试使用selenium获取文本“不正确的凭据” 这就是我尝试过的 message_text = self.driver.find_element(By.XPATH, '//*[@id="toast-container"]/div/div[1][@class="ng-binding toast-title"]') print(message_text.text) 还尝试: message_text = self.driver. find_element_by_xpath('//*[@i

我正在尝试使用selenium获取文本“不正确的凭据”

这就是我尝试过的

message_text = self.driver.find_element(By.XPATH, '//*[@id="toast-container"]/div/div[1][@class="ng-binding toast-title"]')
print(message_text.text)
还尝试:

 message_text = self.driver. find_element_by_xpath('//*[@id="toast-container"]/div/div[1][@class="ng-binding toast-title"]').text
但我只得到一个空字符串

文本仅通过JS添加到页面中

知道为什么它是空白的吗

这是呈现的html

<div ng-repeat="toaster in toasters" class="toast ng-scope toast-error" ng-class="toaster.type"
     ng-click="click(toaster)" ng-mouseover="stopTimer(toaster)" ng-mouseout="restartTimer(toaster)" style="">
    <button class="toast-close-button" ng-show="config.closeButton">×</button>
    <div ng-class="config.title" class="ng-binding toast-title">Incorrect Credentials</div>
    <div ng-class="config.message" ng-switch="" on="toaster.bodyOutputType" class="toast-message">
        <!-- ngSwitchWhen: trustedHtml --><!-- ngSwitchWhen: template --><!-- ngSwitchDefault:  -->
        <div ng-switch-default="" class="ng-binding ng-scope">Incorrect Email/Password</div>
    </div>
</div>
更新

我要和这个一起工作

 element = WebDriverWait(self.driver, 20).until(
            EC.text_to_be_present_in_element((By.CLASS_NAME, "toast-title"), 'Incorrect Credentials')
        )

1我需要先等待AJAX调用完成,然后再检查值

2创建页面时,消息框已呈现为空值

要解决这个问题,我需要等待消息框中的文本更改

 element = WebDriverWait(self.driver, 20).until(
            EC.text_to_be_present_in_element((By.CLASS_NAME, "toast-title"), 'Incorrect Credentials')
        )
元素现在返回True,如果在x秒内未找到文本错误凭据。我可以使用

 noz.assert_equal(element, True)

@Brandon yes和message_text.text的值应为'error Credentials',但为空。我错过什么了吗?你在使用LiveServerTestCase吗?您应该能够使用内置的.find_element_by_id方法,而不是xpath。我已经尝试了self.driver.find_element_by_class_name'toast-title',但没有任何乐趣。也许我需要等待?您肯定需要给ajax/dom操作时间来完成。我个人觉得这种类型的测试更容易处理QUnit测试。@Brandon解决了这个问题,我能让selenium等待http状态码吗?
 noz.assert_equal(element, True)