Selenium,Python - 单击按钮生成笑话后无法从动态网页检索文本
Selenium, Python - Unable to retrieve a text from a dynamic webpage after clicking a button to generate a Joke
我在 python 控制台上检索文本失败。 Python/Selenium 认为它是空白的,因此显示 Failed to obtain text
(如结果图像所示)。
Python代码:
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()
结果如下:
该网站是一个简单的笑话生成器,在单击按钮之前,最初不会显示笑话。单击后,将生成一个笑话。除了代码中显示的 XPATH 之外,我还尝试使用 (By.ID, "p")
,但得到了相同的失败结果。
下面是点击按钮生成笑话前后的网页图片以及各自的源代码。 HTML 中突出显示的是我选择文本定位符 id="p" 的位置。
非常感谢任何帮助。如果您有任何疑问,请随时提问。
点击 Next Joke
按钮后,生成新笑话并展示它需要一些时间。您必须等待文本在该元素内可见。
您正在代码中使用 implicitly_wait
。这将等待元素存在。更好的方法是使用 Expected Conditions 显式等待以等待元素可见性、可点击性等。这些是 Web 元素更成熟的状态,只是元素出现在页面上,而元素可能仍未完全呈现等。
请尝试如下操作:
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#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()
wait = WebDriverWait(b, 20)
b.get("https://elated-benz-84557d.netlify.app/#")
#When the user clicked on the button
wait.until(EC.visibility_of_element_located((By.ID, "next"))).click()
#Then the joke gets generated
m = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='p']"))).text
if m == "":
print("Failed to obtain text.")
else:
print(f"The text is: {m}")
b.quit()
test_joke()
我在 python 控制台上检索文本失败。 Python/Selenium 认为它是空白的,因此显示 Failed to obtain text
(如结果图像所示)。
Python代码:
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()
结果如下:
该网站是一个简单的笑话生成器,在单击按钮之前,最初不会显示笑话。单击后,将生成一个笑话。除了代码中显示的 XPATH 之外,我还尝试使用 (By.ID, "p")
,但得到了相同的失败结果。
下面是点击按钮生成笑话前后的网页图片以及各自的源代码。 HTML 中突出显示的是我选择文本定位符 id="p" 的位置。
非常感谢任何帮助。如果您有任何疑问,请随时提问。
点击 Next Joke
按钮后,生成新笑话并展示它需要一些时间。您必须等待文本在该元素内可见。
您正在代码中使用 implicitly_wait
。这将等待元素存在。更好的方法是使用 Expected Conditions 显式等待以等待元素可见性、可点击性等。这些是 Web 元素更成熟的状态,只是元素出现在页面上,而元素可能仍未完全呈现等。
请尝试如下操作:
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#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()
wait = WebDriverWait(b, 20)
b.get("https://elated-benz-84557d.netlify.app/#")
#When the user clicked on the button
wait.until(EC.visibility_of_element_located((By.ID, "next"))).click()
#Then the joke gets generated
m = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='p']"))).text
if m == "":
print("Failed to obtain text.")
else:
print(f"The text is: {m}")
b.quit()
test_joke()