简体   繁体   English

是否可以自定义 JS 路径以执行对元素的单击(复选框)SELENIUM、Java

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

I am trying to make an automation test script in Selenium using Java.我正在尝试使用 Java 在 Selenium 中制作一个自动化测试脚本。 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.我在页面上的复选框有问题,因为我认为它们是使用 GWT 创建的,并且它们具有动态 id-s,并且所有其他标签或类对它们都是相同的。

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.我以某种方式设法使用 javascriptExecutor 并使用我在元素/复制/复制 JS 路径上右键单击的“标签”标签的 JS 路径来执行点击。 The path I got is: document.querySelector("#ttip > div:nth-child(1) > span > label").click()我得到的路径是: 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?现在的问题是所有复选框都具有相同的 JS 路径,我想知道是否有办法更改 JS 路径以仅影响我想要的复选框? Maybe something like adding indexes to the span and label elements??也许像向跨度和标签元素添加索引之类的东西? -> document.querySelector("#ttip > div:nth-child(1) > span(1) > label(1)").click() -> 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这是我设法使用此代码在 JavascriptExecutor 上执行单击的复选框之一
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>

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM