【发布时间】:2020-07-19 08:34:22
【问题描述】:
使用下面的脚本,我试图从跨度错误文本框中获取错误文本,当未输入名称并且用户单击提交按钮时,从带有下面 HTML 的注册屏幕。(@ 987654321@。但是我最终只得到了文本而不是文本。当我尝试从 chrome 控制台获取文本时,我收到带有 xpath 和 css 选择器的文本“$x(”//span[@class='span -error']")[2]" 和 "$$(".span-error")[2]"。我想要一些关于我缺少什么的指导,我想我已经尝试了我能想到的一切。
测试用例:
- 获取链接 在名字上不输入任何内容,获取验证 error 断言验证错误 Enter an invalid name as ":::" 获取 验证错误(他们似乎有一对)等等
from time import sleep
from conftest import os
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
# LoginScreenSelectors
sel_first_name = "#first-name-su"
sel_last_name = "#last-name-su"
sel_email = "#email-su"
sel_password = "#password-su"
sel_error_message = ".span-error:nth-of-type(2)"
sel_submit = f"""[data-automation-id="signup-submit-btn"]"""
sel_required_error = f"""[data-error="required"]"""
class Login():
'''This will signup to the applcaiton '''
def __init__(self, driver):
self.driver = driver
wait = WebDriverWait(self.driver, 20)
self.first_name = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, sel_first_name))
)
self.last_name = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, sel_last_name))
)
self.email = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, sel_email))
)
self.password = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, sel_password))
)
self.submit = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, sel_submit))
)
def login_to_website(self):
wait = WebDriverWait(self.driver, 10)
print(self.driver.title)
self.first_name.click()
self.first_name.send_keys(" ")
self.first_name.send_keys(Keys.TAB)
self.first_name_error_message = wait.until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, sel_error_message))
)
type(self.first_name_error_message)
error_messages = []
for messages in self.first_name_error_message:
print(error_messages.append(messages.text))
#if i could get this then i could simply get the first index and then keep filtering
it but even that is proving to be difficult.
【问题讨论】: