字符串的棘手连接

时间:2020-05-09 06:45:58

标签: python string selenium string-concatenation

我必须将字符串格式化为特定格式,但不幸的是,所有尝试都失败了。

# 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()

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用.format()

    driver.find_element_by_xpath("//*[@id=\"{0}\"]".format(moveToStep)).click()

答案 1 :(得分:1)

用+联接字符串通常不是最好的主意,python有两种方法format strings

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

或者在Python 3.6+上,您可以使用f-strings

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