Javascript 如何在Selenium中获取JS生成的文本?

Javascript 如何在Selenium中获取JS生成的文本?,javascript,python,selenium,Javascript,Python,Selenium,当用户输入不正确的用户名和密码时,我试图获取页面(js)上的文本“不正确的凭据” login_button.click() self.driver.implicitly_wait(10) # seconds get_conformation_message = self.driver.find_element(By.XPATH, '//*[@id="toast-container"]/div/div[1]') noz.assert_equal(get_conformation_message.t

当用户输入不正确的用户名和密码时,我试图获取页面(js)上的文本“不正确的凭据”

login_button.click()
self.driver.implicitly_wait(10) # seconds
get_conformation_message = self.driver.find_element(By.XPATH, '//*[@id="toast-container"]/div/div[1]')
noz.assert_equal(get_conformation_message.text, "Incorrect Credentials")
我已经设置了10秒的等待时间来确保元素在页面上,但是我的测试仍然失败,因为

+不正确的凭证

这是呈现的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>

×
不正确的凭证
不正确的电子邮件/密码

如何在Selenium中实现这一点?

您可以使用显式等待(在我的情况下,它每次都有效):


我无法对rts的问题发表评论

@@关于如何获取toaster消息的xpath

因为我没有足够的声望点数。答案是:

在单击“登录”按钮之前,请执行以下操作:

  • 打开DevTools->Elements选项卡

  • 在body标记处创建一个断点,或者更好:在试图查找的元素的父标记处,选择break on subtree modifications

  • 按F8键

  • 单击“登录”

  • 然后一步一步走,直到toastr出现

  • 检查toastr并检索xpath/cssSelector


  • 这样,我就可以获得足够的信誉点,这样我就可以对事情进行评论:D

    你确定
    get\u confirmation\u消息
    代表了你需要的对象吗?@@r关于如何获取烤面包机消息的xpath。在我的场景中,烤面包机消息在几秒钟后消失,因此我无法获取烤面包机的xpath。
    <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>
    
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    WebDriverWait(self.driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="toast-container"]/div/div[1][@class="ng-binding toast-title"]'))
    )