Using javascript to set text for a web element - Selenium Web driver

How can I set web element text using java script executor? Or is there any other way to do this?

<a class="selectBox selectBox-dropdown selectBox-menuShowing selectBox-active" style="width: 52px; display: inline-block; -moz-user-select: none;" title="" tabindex="0">
<span class="selectBox-label" style="width: 13px;">10</span>
<span class="selectBox-arrow"/>
</a>

There are two span elements under the tag - this is a drop. The user clicks on the range [2], and a list is displayed that contains data such as 10, 20, 30, 40, etc. The user clicks on the number (element) and is set as the text of the range [1] (In this case, 10). How do I solve this?

I tried Action builder and it does not work. Any other suggestions?

+3
source share
1 answer

If you want to directly change the range text [1], you can use the following code:

String jScript = "var myList = document.getElementsByClassName(\"selectBox-label\");"
    +"myList[0].innerHTML=\"YourNumber\";"; 
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(jScript);

java script, , , [1]. :

WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
+5

All Articles