我需要帮助处理我的密码。我在python中使用Selenium。
elems = driver.find_element_by_xpath("//article[@class='page-container']/section/div[3]")
for job in elems:
job_list.append(job.get_attribute('href'))
print(job_list)
The error is "TypeError: 'WebElement' object is not iterable".
我知道elems的结果是一个WebElement,但是我需要使用find_element_by_xpath,因为我想要一个特定的div,而这个div没有唯一的id。我没有找到一种方法来转换(例如)字符串中的WebElement。您是否知道有另一种方法来拥有这个特定的div,但不使用find_element_by_xpath呢?你还有别的主意来回避这个问题吗?
发布于 2021-10-20 14:10:34
for job in elems:
如果您注意,编译器会认为elems是一个列表。
但是您已经将elems
定义为
elems = driver.find_element_by_xpath("//article[@class='page-container']/section/div[3]")
它将返回一个WebElement而不是WebElement的列表。
问题解释:
注意,find_element_by_xpath
返回一个web元素,其中find_elements_by_xpath
返回一个列表。
Fix : (使用find_elements
而不是find_element
)
elems = driver.find_elements_by_xpath("//article[@class='page-container']/section/div[3]")
for job in elems:
job_list.append(job.get_attribute('href'))
print(job_list)
https://stackoverflow.com/questions/69647511
复制相似问题