使用getPageSource检查网页上是否存在某些文本。我得到错误对象没有属性getPageSou

2024-10-01 13:40:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图验证一些文本,例如“test001”在一个网页上使用驱动程序.getPageSource在Python中,Webdriver。 我得到错误对象没有属性“getPageSource”:

Traceback (most recent call last):
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore \TestCases\AdministrationPage_TestCase.py", line 32, in test_add_Project
    administration_page.add_project()
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore \Pages\admin.py", line 43, in add_project
    test = self.driver.getPageSource
AttributeError: 'WebDriver' object has no attribute 'getPageSource'

看了别人的帖子有类似的问题。他们的回答是使用getPageSource。 我不知道为什么会出错。谢谢你的帮助,谢谢。在

我的代码片段是:

^{pr2}$

Tags: inpytestprojectaddseleniumlineusers
1条回答
网友
1楼 · 发布于 2024-10-01 13:40:32

对我来说似乎是个语法错误,应该是page_source而不是{}。参见doc

试试看

def is_project_text_present(self, text):
        #return str(text) in self.driver.page_source
        try:
            project_list_test = self.driver.page_source
        except NoSuchElementException, e:
            return False
        return "test001" in text # check if the project title text test001 is in the page, return true if it is

然而,我认为,获取完整的页面源代码来测试一个单个的文本并不是一个好主意。只需找到所需的元素并测试元素是否包含您要查找的文本

相关问题 更多 >