gpt4 book ai didi

javascript - Django selenium如何使用find_element获取页面文本

转载 作者:行者123 更新时间:2023-11-28 07:54:00 24 4
gpt4 key购买 nike

我正在尝试使用 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>

更新:

这可行,但真的很糟糕......

    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')
)

最佳答案

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

2) 创建页面时消息框已呈现为空白值。

要解决此问题,我需要等到消息框中的文本发生更改...

 element = WebDriverWait(self.driver, 20).until(
EC.text_to_be_present_in_element((By.CLASS_NAME, "toast-title"), 'Incorrect Credentials')
)
如果在 x 秒内未找到文本 In Correct Credentials

元素现在将返回 True。我可以使用...来检查这一点

 noz.assert_equal(element, True)

关于javascript - Django selenium如何使用find_element获取页面文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com