'Selenium, Python - Unable to retrieve a text from a dynamic webpage after clicking a button to generate a Joke

I was unsuccessful in retrieving a text on my python console. Python/Selenium believe it's blank and thus shows Failed to obtain text(shown in the result image).

Python Code:

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

#Feature: Joke Generator Button

#Scenario: Click button to move onto next jokes.

def test_joke():

#Given the website is displayed with the instruction and button
    b=webdriver.Chrome()
    b.get("https://elated-benz-84557d.netlify.app/#")
    b.implicitly_wait(10)

#When the user clicked on the button
    l=b.find_element(By.ID,"next")
    l.click()

#Then the joke gets generated
    m=b.find_element(By.XPATH,"//*[@id='p']").text
    if m == "":
        print("Failed to obtain text.")
    else:
        print(f"The text is: {m}")


    b.quit()

test_joke()

Result Below:

enter image description here

The website is a simple joke generator, where initially shows no joke until a button is clicked on. After it's clicked, a joke is generated. I had also attempted with (By.ID, "p") , other than the XPATH as shown in the code, and had gotten the same failed result.

Below are the images of the webpage before and after clicking the button to generate a joke as well as their respective source code. Highlighted in the HTML is where I had selected the locator of the text, id="p".

enter image description here

enter image description here

Any help is much appreciated. If you have questions, feel free to ask.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source