Python Selenium调用.text方法无法获取文本问题求助
从Mailinator邮箱提取验证码时Selenium .text方法无法获取文本的问题
我需要从Mailinator邮箱的邮件中提取验证码,但使用Python Selenium的.text方法时,始终无法获取并存储任何文本。此外只能使用find_elements(By.XPATH, "示例")这类新语法,无法使用find_elements_by_xpath("示例")等旧语法。我使用的是最新版本的Python,开发环境为Visual Studio Code。
我尝试过通过正则匹配循环提取符合规则的元素,但结果相同。目前没有报错信息,仅输出换行符,且该问题并非仅出现在Mailinator场景中,测试谷歌首页等多个网站均存在相同问题。以下是移除凭证后的测试代码:
from selenium import webdriver from selenium.webdriver.common.by import By import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # from faker import Faker # import re options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Chrome(options=options) wait = WebDriverWait(driver, 20) username = '*' driver.get("https://www.mailinator.com/v4/login.jsp") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[2]/input"))).send_keys( "*") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[3]/input"))).send_keys( "*") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[4]/a[1]"))).click() wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(@id,'row_"+ username +"')]/td[2]"))).click() iframe = driver.find_element('tag name', 'iframe') print('line21 executed') driver.switch_to.frame(iframe) print('line23 executed') email_text = driver.find_element('tag name', 'body').text print('line25 executed') print(email_text) print('line27 executed')
我需要获取文本以提取验证码,用于项目BDD测试中的注册流程,但该问题已困扰我数小时,无法解决。
编辑1
根据建议更新代码后,输出如下:
from selenium import webdriver from selenium.webdriver.common.by import By import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # from faker import Faker # import re options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Chrome(options=options) wait = WebDriverWait(driver, 20) username = '*' driver.get("https://www.mailinator.com/v4/login.jsp") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[2]/input"))).send_keys( "*") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[3]/input"))).send_keys( "*") wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/section/form/div[4]/a[1]"))).click() wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(@id,'row_"+ username +"')]/td[2]"))).click() iframe = driver.find_element(By.ID, 'html_msg_body') print('line21 executed') driver.switch_to.frame(iframe) print('line23 executed') email_text = wait.until(EC.visibility_of_element_located(('tag name', 'body'))).text print('line25 executed') print(email_text) print('line27 executed')
输出内容:
line21 executed line23 executed line25 executed line27 executed
编辑2
我改用公开的Mailinator邮箱测试,代码如下:
from selenium import webdriver from selenium.webdriver.common.by import By import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # from faker import Faker # import re options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Chrome(options=options) wait = WebDriverWait(driver, 20) driver.get("https://www.mailinator.com/v4/public/inboxes.jsp?to=testingfor042223") wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="row_testingfor042223-1682214679-12524595"]/td[2]'))).click() # wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/main/div[2]/div[3]/div/div[4]/div/div/table/tbody/tr/td[2]"))).click() iframe = driver.find_element(By.ID, 'html_msg_body') print('line21 executed') driver.switch_to.frame(iframe) print('line23 executed') body = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body"))) print('line25 executed') print(body.text) print('line27 executed')
内容的提问来源于stack exchange,提问作者Rush Neighbors
相关产品推荐
相关产品推荐

