gpt4 book ai didi

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

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

我正在尝试验证一些文本,例如在 Python、Webdriver 中使用 driver.getPageSource 的网页上的“test001”。我收到错误对象没有属性“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。我不确定为什么会收到错误消息。感谢一些帮助,谢谢。

我的代码片段是:

class AdministrationPage(BasePage):

def is_project_text_present(self, text):
#return str(text) in self.driver.getPageSource
try:
project_list_test = self.driver.getPageSource
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

class AdministrationPage_TestCase(unittest.TestCase):

try: administration_page.is_text_present("test001")
except AssertionError as e: self.verificationErrors.append(str(e))

最佳答案

对我来说似乎是一个语法错误,它应该是 page_source 而不是 getPageSource。查看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

但是,我认为获取整页源代码来测试单个 文本并不是一个好主意。只需找到预期的元素并测试该元素是否包含您要查找的文本即可

关于python - 使用 getPageSource 检查网页上是否存在某些文本。我得到错误对象没有属性 getPageSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30330594/

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