我有以下 html: 它有一个类和一个自定义属性,我有几个具有相同类名的 header 。我想知道如何唯一地获取这个元素并单击它。
<h4 class="font-white topic-heading progress-header no-margin width-80 d-table-cell" data-collapse-id="1">I. Introduction</h4>
这是我尝试过的:-
我尝试使用 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
我也尝试过:-
var element = driver.findElement(By.xpath("//*[@data-collapse-id='1']"));
element.click(); // element.click is not a function exception.
我想知道如何在selenium中获取这个元素并点击它。
这是整个 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>
最佳答案
你尝试过吗:
driver.findElement(By.cssSelector("h4[data-collapse-id='1']")).click();
通过此属性查找元素应该可行,因为这是唯一的。有时它也无法单击 xpath 找到的元素。我认为这应该有效
关于javascript - 如何点击selenium中的自定义属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44034317/