[英]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.