【发布时间】:2020-09-17 13:20:15
【问题描述】:
使用 Firefox 网络驱动程序,我想从 a href 中提取所有包含单词的 URL。
我正在使用最新的硒二进制文件。
试过这个:
driver = webdriver.Firefox()
driver.get(url)
nodes = driver.find_elements(By.XPATH, "//a[contains(@href,'products')]/@href")
print("nodes: ", nodes)
links = []
for elem in nodes:
links.append(elem)
但出现类型错误:
selenium.common.exceptions.WebDriverException: Message: TypeError: Expected an element or WindowProxy, got: [object Attr href="https://www.example.com/catalogue/products/a.html"]
也试过driver.find_elements(By.XPATH, "//a[contains(@href,'products')]")
然后对每个使用getAttribute("href"),但也不能使用。
不明白错误在哪里以及如何解决。
html 的摘录:
<html>
<body>
<ul class="level2-megamenu">
<li>
<div class="level1-title">
<a href="https://www.example.com/catalogue/products/a.html">
<strong style="color:#828282;font-size:>Text</strong>
</a>
</div>
</li>
</ul>
</body>
</html>
【问题讨论】:
-
用相关的 HTML 更新问题。
标签: python selenium selenium-webdriver selenium-firefoxdriver