如何解决尽管使用了 .text 方法,但在我的 python selenium 中获取 NoneType
我正在尝试根据 List (totalListNum) 的数量计算要运行的 for 循环数
而且它似乎返回 Nonetype 而实际上它应该返回 text 或 int
网站:https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1=68&search_cat2=0
下面的代码
for prefectureValue in prefectureValueStorage:
driver.get(
f"https://stamprally.org/?search_keywords&search_keywords_operator=and&search_cat1={prefectureValue}&search_cat2=0")
# Calculate How Many Times To Run Page Loop
totalListNum = driver.find_element_by_css_selector(
'div.page_navi2.clearfix>p').get_attribute('text')
totalListNum.text.split("件中")
if totalListNum[0] % 10 != 0:
pageLoopCount = math.ceil(totalListNum[0])
else:
continue
currentpage = 0
while currentpage < pageLoopCount:
currentpage += 1
print(currentpage)
解决方法
我认为您不应该使用 get_attribute。不如试试这个。
totalListNum = driver.find_element_by_css_selector('div.page_navi2.clearfix>p').text
,
首先,您的定位器不是唯一的 使用这个:
div.page_navi2.clearfix:nth-of-type(1)>p
或者对于第二个元素:
div.page_navi2.clearfix:nth-of-type(2)>p
第二,如前所述,使用 .text
获取文本。
如果 .text 不起作用,您可以使用 .get_attribute('innerHTML')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。