英文:
Can a JS Path be customized to execute a click on a element (checkbox) SELENIUM, Java
问题
我正在尝试使用Java中的Selenium制作自动化测试脚本。我在页面上的复选框遇到问题,因为它们使用了我认为是GWT创建,并且它们具有动态ID,而其他任何标签或类对于所有复选框都是相同的。
我已经设法使用JavascriptExecutor执行了对第一个复选框的点击操作,并且使用了我通过右键单击元素/复制/复制JS路径得到的'label'标签的JS路径。
我得到的路径是:`document.querySelector("#ttip > div:nth-child(1) > span > label").click()`
现在的问题是,所有复选框都具有相同的JS路径,我想知道是否有办法更改JS路径,以便仅影响我想要的复选框?也许可以像在span和label元素上添加索引之类的东西吗?例如:`document.querySelector("#ttip > div:nth-child(1) > span(1) > label(1)").click()`
1. 这是我使用以下代码成功在JavascriptExecutor上点击的复选框之一:
public void clickChckBoxPretezita() {
js.executeScript("document.querySelector("#ttip > div:nth-child(1) > span > label").click()");
}
```html
<span class="v-checkbox v-widget">
<input type="checkbox" value="on" id="gwt-uid-46" tabindex="0">
<label for="gwt-uid-46" style=""></label>
</span>
- 这是第二个复选框,我无法成功点击它。
<span class="v-checkbox v-widget">
<input type="checkbox" value="on" id="gwt-uid-47" tabindex="0">
<label for="gwt-uid-47" style=""></label>
</span>
<details>
<summary>英文:</summary>
I am trying to make an automation test script in Selenium using Java. I have problems with checkboxes on the page because they are created using GWT I think and they have dynamic id-s, and every other tag or class is the same for all of them.
I have somehow managed to execute a click on the first one using javascriptExecutor and using the JS Path of the 'label' tag that I got right clicking on the element/copy/copy JS Path.
The path I got is: `document.querySelector("#ttip > div:nth-child(1) > span > label").click()`
Now the issue is that all of the checkboxes have the same JS Path and I am wondering if there is a way to change the JS Path to somehow affect only the checkbox that I want? Maybe something like adding indexes to the span and label elements?? -> `document.querySelector("#ttip > div:nth-child(1) > span(1) > label(1)").click()`
1. This is one of the checkboxes that I managed to execute a click over JavascriptExecutor with this code
public void clickChckBoxPretezita() {
js.executeScript("document.querySelector("#ttip > div:nth-child(1) > span > label").click()");
}
<span class="v-checkbox v-widget">
<input type="checkbox" value="on" id="gwt-uid-46" tabindex="0">
<label for="gwt-uid-46" style=""></label>
</span>
1. This is the second one and I can't manage to execute a click on it.
<span class="v-checkbox v-widget">
<input type="checkbox" value="on" id="gwt-uid-47" tabindex="0">
<label for="gwt-uid-47" style=""></label>
</span>
</details>
# 答案1
**得分**: 0
如果您想使用索引进行执行,可以按照以下方法进行操作。
```java
JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("document.querySelector(\"#ttip > div:nth-child(arguments[0]) > span:nth-child(arguments[1]) > label:nth-child(arguments[2])\").click()", divIndex, spanIndex, labelIndex);
英文:
If you want to execute using indexes you can follow below approach.
JavascriptExecutor js = ((JavascriptExecutor)driver)
js.executeScript("document.querySelector(\"#ttip > div:nth-child(arguments[0]) > span:nth-child(arguments[1]) > label:nth-child(arguments[2])\").click()", divIndex,spanIndex,labelIndex);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论