Can a JS Path be customized to execute a click on a element (checkbox) SELENIUM, Java

huangapple go评论83阅读模式
英文:

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>
  1. 这是第二个复选框,我无法成功点击它。
<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 &#39;label&#39; tag that I got right clicking on the element/copy/copy JS Path.
The path I got is: `document.querySelector(&quot;#ttip &gt; div:nth-child(1) &gt; span &gt; label&quot;).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?? -&gt; `document.querySelector(&quot;#ttip &gt; div:nth-child(1) &gt; span(1) &gt; label(1)&quot;).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(&quot;#ttip > div:nth-child(1) > span > label&quot;).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&#39;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(&quot;document.querySelector(\&quot;#ttip &gt; div:nth-child(arguments[0]) &gt; span:nth-child(arguments[1]) &gt; label:nth-child(arguments[2])\&quot;).click()&quot;, divIndex,spanIndex,labelIndex);

huangapple
  • 本文由 发表于 2020年9月16日 15:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63914902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定