如何使用selenium和python从元素中检索值

2024-10-05 14:23:14 发布

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

我是编程初学者。我使用python3selenium从网站的两个元素中获取值。在

我想检索当前和总页数,见下图。在

enter image description here

在我的例子中,当前的页码是43,总页数是510。在

当我检查有问题的元素时,我得到以下源代码:

enter image description here

第一行表示当前页字段,第二行表示总页数。在

我设法检索了这两个元素的xpath,它们是

对于当前页面元素:

'//*[@id="openSDPagerInputContainer2"]/label[2]'

对于total page元素:

^{pr2}$

比如:

totalPages = driver.find_element_by_css_selector('afterInput').get_attribute('innerHTML').split()[1]
currentPage = sys.argv[2]

或者:

totalPages = driver.find_elements_by_xpath('//*[@id="openSDPagerInputContainer2"]/label[2]')
currentPage = driver.find_elements_by_xpath('//*[@id="openSDPagerInputContainer2"]/input')

不起作用。对于第一个代码片段,我得到一个selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: afterInput错误消息,对于第二个代码片段,我得到一个AttributeError: 'list' object has no attribute 'split'错误消息,如果没有.split()我将收到错误的输出。在

如何使用seleniumpython3检索当前和总页码?在


Tags: id元素bydriverselenium错误findxpath