首页 > 解决方案 > Selenium 使用 OR/AND 运算符选择特定内容 - python

问题描述

我正在使用 Python 解决 Selenium 中的一个问题。

这是我所拥有的虚拟草稿。

<body>
    <button info="content1" aria-label="1">"Click 1"</button>
    <button info="content1" aria-label="2">"Click 2"</button>
    <button info="content2" aria-label="2">"Click 2"</button>
    <button info="content2" aria-label="4">"Click 4"</button>
<body>

我的目标是选择具有info="content1"aria-label="2"

我已经试过了

element=driver.find_element_by_css_selector('button[info="content1"] and button[aria-label="2"]')

但不起作用并返回 NoSuchElementException

你能帮帮我吗?

标签: pythonhtmlselenium

解决方案


只需将两个带括号的属性选择器放在一起,没有 no and

element = driver.find_element_by_css_selector('button[info="content1"][aria-label="2"]')

推荐阅读