我想迭代一个 webelements 列表并返回每个元素的文本,但我只从第一个<h2>
元素获取文本,而不是从其他<li>
标签内的其余元素获取文本,然后代码存在该循环
这是我想从中提取文本的 Html 代码的一部分:
<div class="KambiBC-event-page-component__column KambiBC-event-page-component__column--1">
<ul class="KambiBC-list-view__column">
<li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
<header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
<h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Piete selectate</h2>
</header>
</li>
<li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
<header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
<h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Another text</h2>
</header>
</li>
<li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
<header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
<h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Different text</h2>
</header>
</li>
<li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">
<header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">
<h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Yet another text</h2>
</header>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
这是Python代码:
import time
from …
Run Code Online (Sandbox Code Playgroud)我想找到包含某个类名的所有元素,但跳过那些除了我正在搜索的类名之外还包含另一个类名的元素
我有元素<div class="examplenameA">
和元素<div class="examplenameA examplenameB">
目前我正在这样做来克服我的问题:
items = driver.find_elements_by_class_name('examplenameA')
for item in items:
cname = item.get_attribute('class')
if 'examplenameB' in cname:
pass
else:
rest of code
Run Code Online (Sandbox Code Playgroud)
我只想要具有类名的元素examplenameA
,并且我想跳过那些也包含的元素examplenameB
我试图span
通过遍历li
此 HTML 列表来获取标签内的文本:
<ol class="KambiBC-event-result__score-list">
<li class="KambiBC-event-result__match">
<span class="KambiBC-event-result__points">1</span>
<span class="KambiBC-event-result__points">1</span>
</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
但我收到错误
AttributeError: 'list' 对象没有属性 'find_element_by_class_name'
在我的代码上:
meci = driver.find_elements_by_class_name('KambiBC-event-result__match')
for items in meci:
scor = meci.find_element_by_class_name('KambiBC-event-result__points')
print (scor.text)
Run Code Online (Sandbox Code Playgroud)