如何通过Python中标签的属性选择元素

2024-10-01 07:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在python中使用selenium访问多个标记的文本。你知道吗

标记没有idclass这样的属性;它们有一个名为itemprop的属性。你知道吗

例如,有多个这样类型的标记:

<p itemprop="articleBody">
London's Gatwick Airport ........</p>

我不能使用“selectelementbytagname”,因为我不想包含具有不同属性的标记“p”。你知道吗

我使用下面的代码来选择这些元素:

elements = driver.find_element(By.CSS_SELECTOR, """p[itemprop='articleBody’]""")

但是它抛出错误-。。。。。。你知道吗

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"p[itemprop='articleBody’]"}

我怎样才能解决这个问题?你知道吗


Tags: 标记文本id类型属性seleniumelementselector
1条回答
网友
1楼 · 发布于 2024-10-01 07:37:18

在选择器中有智能引号,使用find_elements*s来获取多个元素。你知道吗

elements = driver.find_elements(By.CSS_SELECTOR, 'p[itemprop="articleBody"]')
# Or
elements = driver.find_elements_by_css_selector('p[itemprop="articleBody"]')

相关问题 更多 >