python - Python/Selenium - 不知道如何解决循环问题
问题描述
嗨,我正在学习 python/selenium 并使用页面对象模型练习自动化测试用例。
我正在尝试在此演示网站https://admin-demo.nopcommerce.com/Admin/Category/List上自动化此页面
管理员邮箱:admin@yourstore.com 管理员密码:admin
在我的测试用例中,我在列出所有类别的表中添加了一个新类别。为了测试我的代码是否健壮,我决定将新类别添加到表的末尾。因此,您必须转到最后一页才能查看已添加的类别。
这是我的 CategoriesPage.py 文件,我在其中调用了我想在这些选择器上实现的所有选择器和方法。
我已经整理好了,所以它只包含与问题相关的代码。
class Categories:
tblrows_css = "tbody>tr"
numOfPages_css = "#categories-grid_paginate > ul > li"
def __init__(self, driver):
self.driver = driver
def getRows(self):
num_of_rows = self.driver.find_elements_by_css_selector(self.tblrows_css)
return num_of_rows
def getPages(self):
num_of_pages = self.driver.find_elements_by_css_selector(self.numOfPages_css)
return num_of_pages
def pageClick(self, page_num):
#self.driver.find_element_by_css_selector("#categories-grid_paginate > ul > li:nth-child(" + str(page_num) + ")").click()
self.driver.find_element_by_link_text(str(page_num)).click()
def checkList(self, exp_name): #where exp_name is a variable set to 'test123' in the test file
flag = False
num_of_pages = self.getPages()
print("num of pages is " + str(len(num_of_pages)))
for page in range(1, len(num_of_pages) - 1): # don't include arrow buttons, loops through each page until Name is found in table
self.pageClick(page)
print("we are on page " + str(page))
num_of_rows = self.getRows()
print("num of rows is "+ str(len(num_of_rows)))
for item in range(1, len(num_of_rows) + 1):
act_name = self.driver.find_element_by_css_selector("#categories-grid > tbody > tr:nth-child(" + str(item) + ") > td:nth-child(2)").text
print(item, act_name)
if exp_name == act_name:
flag = True
print("it works")
break
return flag
所以我有 1 个小问题和 1 个大问题。
小问题是
def pageClick(self, page_num):
#self.driver.find_element_by_css_selector("#categories-grid_paginate > ul > li:nth-child(" + str(page_num) + ")").click()
self.driver.find_element_by_link_text(str(page_num)).click()
我无法让我注释掉的那行工作。所以我不得不找到一个解决方法并使用linktext所以'1'用于第1页,'2'用于第2页等。但是,如果页面上的其他任何地方有一个链接'1',这种方法将不再工作。
我使用 css_selector 得到的错误是
E selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size
E (Session info: chrome=87.0.4280.88)
venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py:242: ElementNotInteractableException
更大的问题是 checkList() 方法。我已经放了一些打印语句来帮助我弄清楚发生了什么。
我正在尝试遍历第二列中的所有行并获取类别的名称(act_name)并查看它是否与 exp_name 匹配。一旦完成,它将打破循环,flag = True,当我在测试中断言 flag 时,它将返回 True 并通过。如果没有,那么它将转到第 2 页并再次遍历表格以查找第 2 页上的所有项目等。
问题是当我运行这个方法时,它会打印出第 1 页上的所有内容两次。
num of pages is 4
we are on page 1
num of rows is 15
1 Computers
2 Computers >> Desktops
3 Computers >> Notebooks
4 Computers >> Software
5 Electronics
6 Electronics >> Camera & photo
7 Electronics >> Cell phones
8 Electronics >> Others
9 Apparel
10 Apparel >> Shoes
11 Apparel >> Clothing
12 Apparel >> Accessories
13 Digital downloads
14 Books
15 Jewelry
we are on page 2
num of rows is 15
1 Computers
2 Computers >> Desktops
3 Computers >> Notebooks
4 Computers >> Software
5 Electronics
6 Electronics >> Camera & photo
7 Electronics >> Cell phones
8 Electronics >> Others
9 Apparel
10 Apparel >> Shoes
11 Apparel >> Clothing
12 Apparel >> Accessories
13 Digital downloads
14 Books
15 Jewelry
FAILED
出于某种原因,当我们点击打印语句“我们在第 2 页”时,我们显然仍然在第 1 页上。然后它再次打印出第 1 页上的所有内容。但是当测试失败时,我可以看到我们点击了第 2 页。
谁能弄清楚我哪里出错了?
解决方案
推荐阅读
- c - 如何处理 C 中输入的数字太大?
- python - 如何通过匹配某个元素来填充二维数组的所有行?
- angular - 关于测试 Angular $localize (Angular 9) 的建议
- amazon-web-services - Boto3 SES 抛出 TypeError: sequence item 0: expected str instance, tuple found
- javascript - 如何使用 d3.js 以编程方式触发拖动事件?
- python - 如何使用 python webbot 获取包含标签的文本?
- sql - SQL SCCM 数据库服务器通过证书解密
- powershell - Powershell:从外部域导出组成员
- javascript - Formik 和 ReactJs:从子组件到父组件的 SetFieldValue
- c++ - 如何正确释放 Mysql C++ 连接器中的内存?