简体   繁体   中英

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. 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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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