简体   繁体   English

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

[英]How to get text generated by JS in Selenium?

I am trying to get the text "Incorrect Credentials" which is placed on page (js) when the user enters an incorrect user name and password...当用户输入不正确的用户名和密码时,我试图获取放置在页面 (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")

I have set a wait time of 10 seconds to make sure the element is on the page, but my test still fails with....我已经设置了 10 秒的等待时间以确保元素在页面上,但我的测试仍然失败......

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

+ Incorrect Credentials + 不正确的凭据

This is the rendered html...这是渲染的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>

How can I get this to work in Selenium?我怎样才能让它在 Selenium 中工作?

You can use an explicit waiting (it works every time in my case):您可以使用显式等待(在我的情况下每次都有效):

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

I'm unable to comment on rts's question我无法评论 rts 的问题

@@erthalion How to get the xpath of toaster messages. @@erthalion 如何获取烤面包机消息的 xpath。

because I don't have enough reputation points.因为我没有足够的声望点。 The answer is:答案是:

Right before clicking the login button do this:在单击登录按钮之前执行以下操作:

  1. Open DevTools -> Elements tab打开 DevTools -> Elements 选项卡

  2. Create a breakpoint at the body tag, or even better: the parent tag of the element you are trying to find, select break on subtree modifications在 body 标签处创建一个断点,或者甚至更好:您要查找的元素的父标签,在子树修改时选择中断

  3. Press F8按F8

  4. Click "Login"点击“登录”

  5. Then step through until the toastr is in view然后一步一步直到烤面包机出现在视野中

  6. Inspect the toastr and retrieve xpath/cssSelector检查 toastr 并检索 xpath/cssSelector

Like so I can get enough reputation points so I can actually comment on things :D像这样我可以获得足够的声望点,这样我就可以对事情发表评论:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM