Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium Xpath获取文本_Python_Selenium_Selenium Webdriver_Xpath_Ui Automation - Fatal编程技术网

Python Selenium Xpath获取文本

Python Selenium Xpath获取文本,python,selenium,selenium-webdriver,xpath,ui-automation,Python,Selenium,Selenium Webdriver,Xpath,Ui Automation,请告诉我如何才能找到文本无效的电子邮件地址。在此xpath中: driver.find_element_by_xpath(".//*[@id='create_account_error']/ol/li") 并使用断言进行验证 您可以查看此网站- 在“创建帐户”选项卡中输入错误的电子邮件地址,然后单击“创建帐户”,您将看到错误消息 如何使用xpath验证此错误消息以生成示例测试用例?我将为您提供示例代码,这些代码可以满足您的需要,并将其相应地放入测试用例中 from selenium impo

请告诉我如何才能找到文本无效的电子邮件地址。在此xpath中:

 driver.find_element_by_xpath(".//*[@id='create_account_error']/ol/li")
并使用断言进行验证

您可以查看此网站-

在“创建帐户”选项卡中输入错误的电子邮件地址,然后单击“创建帐户”,您将看到错误消息


如何使用xpath验证此错误消息以生成示例测试用例?

我将为您提供示例代码,这些代码可以满足您的需要,并将其相应地放入测试用例中

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

url="http://automationpractice.com/index.php?controller=authentication"

driver =webdriver.Chrome()
driver.maximize_window()

driver.get(url)

driver.find_element(By.ID,'email_create').send_keys("This is Test")

driver.find_element(By.ID,'SubmitCreate').click()

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#create_account_error > ol > li")))

ele=driver.find_element(By.CSS_SELECTOR,'#create_account_error > ol > li')
assert ele.text == 'Invalid email address.'

driver.quit()
试试这个:

expected_text = 'Invalid email address.'
elem = driver.find_element_by_xpath(".//*[@id='create_account_error']/ol/li")
assert elem.text == expected_text

希望它能对您有所帮助。

您好,欢迎来到SO。请仔细阅读发布问题的指南: