我正在尝试使用selenium获取文本'Incorrect Credentials'。
这是我试过的......
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"))
)
更新
有了这个......
element = WebDriverWait(self.driver, 20).until(
EC.text_to_be_present_in_element((By.CLASS_NAME, "toast-title"), 'Incorrect Credentials')
)
答案 0 :(得分:0)
1)在检查值之前,我需要先等待AJAX调用完成。
2)在创建页面时,消息框已经显示为空白值。
要解决这个问题,我需要等到消息框中的文字改变了......
element = WebDriverWait(self.driver, 20).until(
EC.text_to_be_present_in_element((By.CLASS_NAME, "toast-title"), 'Incorrect Credentials')
)
如果在x秒内未找到文本True
,则元素现在返回Incorrect Credentials
。我可以使用......来检查这个...
noz.assert_equal(element, True)