如何点击selenium中的自定义属性?

kus*_*hal 2 javascript selenium xpath nodes

我有以下 html:它有一个类和一个自定义属性,我有几个具有相同 className 的标头。我想知道如何唯一地获取这个元素并单击它。

<h4 class="font-white topic-heading progress-header no-margin width-80 d-table-cell" data-collapse-id="1">I. Introduction</h4>
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的:-

我尝试使用 data-collapse-id="1" 获取 class="font-white..." 的属性:

var element = driver.findElement(By.xpath("//*[@class='font-white topic-heading progress-header no-margin width-80 d-table-cell']")).getAttribute('data-collapse-id="1"');
console.log(element); // this prints a promise.

element.click(); //element.click is not a function exception 
Run Code Online (Sandbox Code Playgroud)

我也尝试过:-

var element = driver.findElement(By.xpath("//*[@data-collapse-id='1']"));

element.click(); // element.click is not a function exception.
Run Code Online (Sandbox Code Playgroud)

我想知道如何在硒中获取这个元素并单击它。

这是整个 div:

<div class="page-width d-table topic-heading-div">
   <h4 class="font-white topic-heading progress-header no-margin width-80 d-table-cell" data-collapse-id="1">I. Introduction</h4>
   <i class="fa fa-check font-white font-18 width-20 d-table-cell text-center vertical-center" aria-hidden="true"></i>
</div>
Run Code Online (Sandbox Code Playgroud)

小智 5

你试过了吗:

driver.findElement(By.cssSelector("h4[data-collapse-id='1']")).click();
Run Code Online (Sandbox Code Playgroud)

通过此属性查找元素应该可行,因为这是唯一的。有时它也无法单击 xpath 找到的元素。我认为这应该有效