[英]Django selenium how to get page text using find_element
我正在嘗試使用硒獲取文本“不正確的憑據”。
這就是我嘗試過的...
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>
更新:
這可行,但確實很糟糕。
self._login_process()
import time
time.sleep(10)
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "toast-title"))
)
UPDATE
可以使用...
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')
)
如果在x秒內未找到文本“ Incorrect Credentials
則元素現在返回True
。 我可以使用...檢查
noz.assert_equal(element, True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.