Search code examples
pythonstringseleniumstring-concatenation

A tricky concatenation of strings


I have to format a string to be in a specific form, but unfortunately all my attempts have failed..

# What I want:
//*[@id="ember205"]

# What I am getting:
//*[@id=ember205]

# Additional details, where I need it and the way I am constructing it:
moveToStep = str("ember"+str(int(199)+int(step_number * 3)))
driver.find_element_by_xpath("//*[@id="+moveToStep+"]").click()

Any help would be really appreciated


Solution

  • Joining strings with + usually isn't the best idea, python has a couple of ways to format strings.

    moveToStep = 199 + step_number * 3
    driver.find_element_by_xpath('//*[@id="ember{}"]'.format(moveToStep)).click()
    

    or on Python 3.6+ you can use f-strings

    moveToStep = 199 + step_number * 3
    driver.find_element_by_xpath(f'//*[@id="ember{moveToStep}"]').click()