1

初始情况:

目前我已经在我们的项目中定义了几个测试用例,包括在 Magento 云环境中的登录和注销。

我目前使用 Chrome Webdriver 进行此测试。

Python 和最新的 Selenium 版本。

问题情况: 我想检查一个用户是否已经注册。

到目前为止我所拥有的:

我目前正在检查用户是否要登录,如果是相应的用户“Frank”,则会触发断言。

但我相信有更好的解决方案?

 def test_login(self):
        driver = self.driver 
        time.sleep(10) 
        driver.find_username = wait.until(EC.presence_of_element_located((By.XPATH,'//span[contains(text(), "Frank")]')))
        assert find_username

问题

是否有更明智的解决方案如何在 Python/Selenium 中进行查询?

4

1 回答 1

1

你很亲密。而不是使用presence_of_element_located()您需要使用visibility_of_element_located(),您可以使用以下定位器策略

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

def test_login(self):       
    try:
        driver.find_username = wait.until(EC.visibility_of_element_located((By.XPATH,'//span[contains(., "Frank")]')))
        print("User was found")
        assert find_username
    except TimeoutException:
        print("User wasn't found")
于 2019-12-04T11:21:56.417 回答