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

时间:2014-10-13 12:04:46

标签: 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.text, "Incorrect Credentials")

我设置了10秒的等待时间,以确保元素在页面上,但我的测试仍然失败....

noz.assert_equal(get_conformation_message.text, "Incorrect Credentials") nose.proxy.AssertionError: '' != 'Incorrect Credentials'
     

+不正确的凭据

这是渲染的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中使用它?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我无法评论rts的问题

  

@@ erthalion如何获取烤面包机消息的xpath。

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

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

  1. 打开DevTools->“元素”标签

  2. 在body标签处创建一个断点,或者甚至更好:在您要查找的元素的父标签处,选择在子树修改时中断

  3. 按F8

  4. 单击“登录”

  5. 然后逐步操作,直到看到烤面包机为止

  6. 检查烤面包机并检索xpath / cssSelector

就像这样,我可以获得足够的声望点,所以我可以对事物进行评论:D