gpt4 book ai didi

python - 如何在selenium python中查找子元素

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:23 25 4
gpt4 key购买 nike

我想找到“行项目”的子元素并将它们存储在有序字典形式中,然后将其传递到 Excel。下面给出的 HTML 和我的代码

<div class = "col-md-5 col-sm-5"
<h3>....</h3>
<div class = "row item">
::before
<div class = "col-md-6 subtitle"> title</div>
<div class = "col-md-6 ng-binding"> 1233344</div>
::after
</div>
<div class = "row item">
::before
<div class = "col-md-6 subtitle"> name</div>
<div class = "col-md-6 ng-binding"> abc</div>
::after
</div>

类似地,我有 23 个“行项目”类的 div,我希望这些值采用有序字典的形式,例如

 Items_dict = {title:1233344, name:abc}

我的代码:

    for rowitem in  driver.find_elements_by_xpath('//div[@class="row item"]'):
titles = rowitem.find_element_by_xpath('.//div[@class="row item"]/div[1]')
values = rowitem.find_element_by_xpath('//div[@class="row item"]/div[2]')

for title,value in zip(titles,values):
items_dict[title] = []
if(title.text and value.text):
items_dict[title.text].append(value.text)

最佳答案

通过删除第一个 for 循环来尝试以下代码:

titles = driver.find_element_by_xpath('.//div[@class="row item"]/div[1]')
values = driver.find_element_by_xpath('//div[@class="row item"]/div[2]')
items_dict={};

for title,value in zip(titles,values):
items_dict[title.get_attribute('text')] = value.get_attribute('text')

关于python - 如何在selenium python中查找子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211454/

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