gpt4 book ai didi

python - 使用 Selenium/Python 验证 div 中是否存在文本时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:50 24 4
gpt4 key购买 nike

所以我试图验证文本是一个元素,基本上我正在测试找不到搜索结果时会发生什么。但是我每次都会收到以下错误消息,但我无法弄清楚原因。

Traceback (most recent call last):
File "test.py", line 40, in test_article_no_result_search
assert article_results_page.is_articles_not_found(), "Articles found surprisingly."
File "/Users/tester/Documents/Automated Tests/foobar/page.py", line 71, in is_articles_not_found
return "No Results Available" in element.get_attribute("value")
TypeError: argument of type 'NoneType' is not iterable

我正在尝试验证的 HTML 元素

<div class="simple-div results-num-span" data-node="group_0.SimpleDiv_0">No Results Available</div>

这是我的 test.py 测试用例

class SearchTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(TestingURLS.URL)

def test_article_no_result_search(self):
main_page = MainPage(self.driver)
main_page.load_page()
main_page.click_article_search_input_clear()
main_page.enter_no_result_search_term()
main_page.click_article_search_button()
article_results_page = ArticleResultsPage(self.driver)
article_results_page.load_page()
assert article_results_page.is_articles_not_found(), "Articles found surprisingly."

def tearDown(self):
self.driver.quit

page.py中相关函数

def is_articles_not_found(self):
element = self.driver.find_element(*SearchResultLocators.UPPER_RESULT_DISPLAY)
return "No Results Available" in element.get_attribute("value")

来自locators.py的相关定位器

class SearchResultLocators(object):
UPPER_RESULT_DISPLAY = (By.CSS_SELECTOR, "div.simple-div.results-num-span")
RESULT_COUNT = (By.CSS_SELECTOR, "div.num-shown")
FIRST_ARTICLE_RESULT = (By.CSS_SELECTOR, "div.result")

最佳答案

element.get_attribute("value") 可以应用于 "text" 类型的 input 节点。在您的情况下,它是带有子文本节点的 div ,因此您可以执行以下断言:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.common.exceptions import TimeoutException

def is_articles_not_found(self):
element = self.driver.find_element(*SearchResultLocators.UPPER_RESULT_DISPLAY)
try:
return wait(self.driver, 3).until(lambda driver: element.text == "No Results Available")
except TimeoutException:
return False

关于python - 使用 Selenium/Python 验证 div 中是否存在文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48505979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com