pythonhtmlseleniumwebautomationwebbot

Selenium(PYTHON) add variable that converts number into syntax


So i have a code that changes the text of a class, but instead of manually entering the number, i would like the number to be equal to whatever X is, and convert that number into a string if thats what needs to be done

try:
    
newButton = WebDriverWait(driver, 100).until(
EC.presence_of_element_located((By.CLASS_NAME, "btn-full")))

finally:

driver.execute_script('arguments[0].innerHTML = "X";', newButton)
driver.execute_script("arguments[0].setAttribute('class','NEWBUTTON')", newButton)

How do i make sure its made in such a way that it takes a variable that holds an integer value, and its written out in place of the X


Solution

  • Well you can pass it as arguments[1] like this:

    x = #some number
    driver.execute_script('arguments[0].innerHTML = arguments[1];', newButton, x)
    

    If you want it to be an int you can leave it as it is, but if you want it as a string then just convert it wherever you want and pass it into the method.

    The arguments[n] is the (n+2)'nd argument you pass into the method execute_script().