python - 无法使用 selenium 定位元素列表

标签 python selenium web-scraping

我需要抓取一些页面。我想要的部分的具体结构如下:

<div class="someclasses">
<h3>...</h3> # Not needed
<ul class="ul-class1 ul-class2">
<li id="li1-id" class="li-class1 li-class2">
<div id ="div1-id" class="div-class1 div-class2 ... div-class6">
<div class="div2-class">
    <div class="div3-class">...</div> #Not needed
    <div class="div4-class1 div4-class2 div4-class3">
        <a href="href1" data-control-id="id1" data-control-name="name" id ="a1-id" class="a-class1 a-class2">
            <h3 class="h3-class1 h3-class2 h3-class3">Text1</h3>
        </a></div>
    <div>...</div> # Not needed
</div>
</li>
<li id="li2-id" class="li-class1 li-class2">
<div id ="div2-id" class="div-class1 div-class2 ... div-class6">
<div class="div2-class">
    <div class="div3-class">...</div> #Not needed
    <div class="div4-class1 div4-class2 div4-class3">
        <a href="href2" data-control-id="id2" data-control-name="name" id ="a2-id" class="a-class1 a-class2">
            <h3 class="h3-class1 h3-class2 h3-class3">Text2</h3>
        </a></div>
    <div>...</div> # Not needed
</div>
</li>
# More <li> elements
</ul>
</div>

现在我想要的是获取Texts以及hrefs。我在上面的示例中使用了完全真实的命名,即相同的名称也是相同的在真实的网页中。我当前使用的代码是:

elems = driver.find_elements_by_xpath("//div[@class='someclasses']/ul[@class='ul-class1']/li[@class='li-class1']")
print(len(elems))
for elem in elems:
    elem1 = driver.find_element_by_xpath("./a[@data-control-name='name']")
    names2.append(elem1.text)
    print(elem1.text)
    hrefs.append(elem.get_attribute("href"))

上面的print语句的结果是0,所以基本上没有找到元素。谁能告诉我我做错了什么。

最佳答案

您仅使用了部分类名...在 XPATH 中您需要完整的类名...

仅供引用:使用 CSS,您可以使用类名的一部分...

如果你想使用 XPATH 尝试:

elems = driver.find_elements_by_xpath("//div[@class='someclasses']//li//a")
print(len(elems))
for elem in elems:

    names2.append(elem.text)
    print(elem.text)
    new_href = elem.get_attribute("href")
    print(new_href)
    hrefs.append(new_href)

对于 CSS 使用:div.someclasses ul.ul-class1

elems = driver.find_elements_by_css_selector("div.someclasses ul.ul-class1 li a")
for elem in elems:
    names2.append(elem.text)
    print(elem.text)
    new_href = elem.get_attribute("href")
    print(new_href)
    hrefs.append(new_href)

关于python - 无法使用 selenium 定位元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248373/

相关文章:

python - 如何使用 "where in"子句

selenium - com.cucumber.listener.ExtentCucumberFormatter 中的初始化错误

python - Scrapy爬虫没有返回预期的html

python - 如何在不滚动的情况下获取整个页面的 HTML?

python - 对非结构化列表中的日期字符串和关联值的数据进行格式化

python - 尝试使用 mysql python 连接器执行准备好的语句时出现 NotImplementedError

python - 如何从 pandas 数据框中获取 'create' 脚本?

ios - 无法在 IOS 模拟器上启动应用程序 - WebDriver 代理错误

java - 无法执行目标 org.apache.maven.plugins :maven-surefire-plugin:3. 0.0-M3:test

python - 从键创建字典时不对键进行排序