如何使用 Selenium 和 Python 使用 find_elements_by_class_name() 单击元素
How to click on element using find_elements_by_class_name() using Selenium and Python
我必须关注我正在尝试抓取的网站上的 HTML:
<div class="test-section-container">
<div>
<span class="test-section-title">Section Title</span>
<div style="display: inline-block; padding: 0.05rem;"></div>
</div>
<div style="cursor: pointer; background-color: rgb(248, 248, 248); display: flex; line-height: 1.2; margin-bottom: 0.07rem;">
<div style="width: 0.5rem; flex-shrink: 0; background-color: rgb(245, 222, 136);"></div>
<div style="padding: 0.07rem; overflow: hidden;">
<div style="font-size: 0.18rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">Newsletter 1</div>
<div style="font-size: 0.13rem; color: rgb(102, 102, 102);">2021 11 8</div>
</div>
</div>
<div style="cursor: pointer; background-color: rgb(248, 248, 248); display: flex; line-height: 1.2; margin-bottom: 0.07rem;">
<div style="width: 0.5rem; flex-shrink: 0; background-color: rgb(221, 221, 221);"></div>
<div style="padding: 0.07rem; overflow: hidden;">
<div style="font-size: 0.18rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">Newsletter 2 </div>
<div style="font-size: 0.13rem; color: rgb(102, 102, 102);">2021 11 3</div>
</div>
</div>
这是我正在使用的 selenium/python 代码:
driver.get("http://www.testwesbite.org/#/newsarticles")
results = driver.find_elements_by_class_name('test-section-container')
texts = []
for result in results:
text = result.text
texts.append(text)
print(text)
这让我输出关闭:
Newsletter 1
2021 11 8
Newsletter 2
2021 11 3
如果我使用下面的代码:
first_result = results[0]
first_result.click()
它确实点击了第一篇文章,但是 results[1]
给我一个越界错误。
我怎么去点击第二篇文章?
因为您已经使用了driver.find_elements_by_class_name('test-section-container')
以下所有文本:
- 时事通讯 1
- 2021 年 11 月 8 日
- 时事通讯 2
- 2021 11 3
在 results[0]
元素中并且 results[1]
不存在。因此你面临越界错误
解决方案
要单击每个 results[0]
和 results[1]
,您可以使用:
driver.get("http://www.testwesbite.org/#/newsarticles")
results = driver.find_elements(By.CSS_SELECTOR, "div.test-section-container div[style*='nowrap']")
texts = []
for result in results:
text = result.text
texts.append(text)
print(text)
现在您可以点击单个项目:
first_result = results[0]
first_result.click()
和
second_result = results[1]
second_result.click()
注意:您必须添加以下导入:
from selenium.webdriver.common.by import By
我必须关注我正在尝试抓取的网站上的 HTML:
<div class="test-section-container">
<div>
<span class="test-section-title">Section Title</span>
<div style="display: inline-block; padding: 0.05rem;"></div>
</div>
<div style="cursor: pointer; background-color: rgb(248, 248, 248); display: flex; line-height: 1.2; margin-bottom: 0.07rem;">
<div style="width: 0.5rem; flex-shrink: 0; background-color: rgb(245, 222, 136);"></div>
<div style="padding: 0.07rem; overflow: hidden;">
<div style="font-size: 0.18rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">Newsletter 1</div>
<div style="font-size: 0.13rem; color: rgb(102, 102, 102);">2021 11 8</div>
</div>
</div>
<div style="cursor: pointer; background-color: rgb(248, 248, 248); display: flex; line-height: 1.2; margin-bottom: 0.07rem;">
<div style="width: 0.5rem; flex-shrink: 0; background-color: rgb(221, 221, 221);"></div>
<div style="padding: 0.07rem; overflow: hidden;">
<div style="font-size: 0.18rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">Newsletter 2 </div>
<div style="font-size: 0.13rem; color: rgb(102, 102, 102);">2021 11 3</div>
</div>
</div>
这是我正在使用的 selenium/python 代码:
driver.get("http://www.testwesbite.org/#/newsarticles")
results = driver.find_elements_by_class_name('test-section-container')
texts = []
for result in results:
text = result.text
texts.append(text)
print(text)
这让我输出关闭:
Newsletter 1
2021 11 8
Newsletter 2
2021 11 3
如果我使用下面的代码:
first_result = results[0]
first_result.click()
它确实点击了第一篇文章,但是 results[1]
给我一个越界错误。
我怎么去点击第二篇文章?
因为您已经使用了driver.find_elements_by_class_name('test-section-container')
以下所有文本:
- 时事通讯 1
- 2021 年 11 月 8 日
- 时事通讯 2
- 2021 11 3
在 results[0]
元素中并且 results[1]
不存在。因此你面临越界错误
解决方案
要单击每个 results[0]
和 results[1]
,您可以使用:
driver.get("http://www.testwesbite.org/#/newsarticles")
results = driver.find_elements(By.CSS_SELECTOR, "div.test-section-container div[style*='nowrap']")
texts = []
for result in results:
text = result.text
texts.append(text)
print(text)
现在您可以点击单个项目:
first_result = results[0]
first_result.click()
和
second_result = results[1]
second_result.click()
注意:您必须添加以下导入:
from selenium.webdriver.common.by import By