我在浏览器上显示了一个带有以下标签的按钮,该标签没有 ID 或单个 CSS 类名。如何找到 Webelement 并单击此按钮。
<button aria-describedby="notes" aria-label="Notes" autofocus="" class="btn btn-primary" role="dialog" type="button">OK</button>
我在浏览器上显示了一个带有以下标签的按钮,该标签没有 ID 或单个 CSS 类名。如何找到 Webelement 并单击此按钮。
<button aria-describedby="notes" aria-label="Notes" autofocus="" class="btn btn-primary" role="dialog" type="button">OK</button>
Web 元素可以通过 XPath 或 CSS 选择器根据任何属性值或 / 和标记名称的任何唯一组合以及它们的父元素或子元素来定位。
由于您没有共享该页面的整个 HTML,我们只能猜测该元素的正确唯一定位器。
使用 XPath,它可能类似于:
"//button[text()='OK']"
或者
"//button[@aria-label='Notes']"
在 CSS 选择器样式中,它将是
"button[aria-label='Notes']"
考虑通过标签名称定位元素。转到具有 id 或类名的父元素并通过标签名 ( parentelement.find_element_by_tag_name('button')) 获取子元素,这是您的按钮
除此之外,您示例中的按钮有两个类。
.click()您通过命令单击按钮。