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

2024-06-02 08:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到文本“不正确的凭据”,这是在页面(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'

+ Incorrect Credentials

这是呈现的html。。。在

^{pr2}$

我怎样才能让它在硒中发挥作用?在


Tags: text文本selfdivmessagegetdriverjs
2条回答

我无法评论rts的问题

@@erthalion How to get the xpath of toaster messages.

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

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

  1. 打开“开发工具”->;“元素”选项卡

  2. 在body标记处创建一个断点,甚至更好:您要查找的元素的父标记,选择break on subtree modifications

  3. 按F8

  4. 单击“登录”

  5. 然后逐步执行,直到看到toastr

  6. 检查toastr并检索xpath/cssSelector

这样我就可以得到足够的声誉点数,这样我就可以对事情发表评论了:D

您可以使用显式等待(在我的案例中每次都有效):

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

相关问题 更多 >