【问题标题】:Python Selenium Xpath get textPython Selenium Xpath 获取文本
【发布时间】:2017-04-07 05:39:57
【问题描述】:

请问我如何在这个 xpath 中找到文本 Invalid email address.

 driver.find_element_by_xpath(".//*[@id='create_account_error']/ol/li")

并用断言验证?

您可以查看这个网站 - http://automationpractice.com/index.php?controller=authentication

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

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

【问题讨论】:

标签: python selenium selenium-webdriver xpath ui-automation


【解决方案1】:

试试这个:

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

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我给你的示例代码可以满足你的需要,把它放在一个测试用例中。

    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()
    

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2020-12-29
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2022-11-16
      • 2018-04-24
      相关资源
      最近更新 更多