Reputation: 24500
i have xpath that looks like this:
"//*[@id="comments_408947"]/div[2]/div[2]/div[2]"
the comment_408947 must be wrapped up with quotes as well as the entire xpath.
i have the number 408947 as a string and need to add it after 'comments_'
com_id = '408947'
query= f("//*[@id=comments_" + """%s""" + "]/div[2]/div[2]/div[2]", com_id)
but it's not working.
Upvotes: 0
Views: 307
Reputation: 51623
"//*[@id="comments_408947"]/div[2]/div[2]/div[2]"
is invalid - you need to use different quotes to allow "
as inside characters - or you need to escape them:
'''"//*[@id="comments_408947"]/div[2]/div[2]/div[2]"''' # works - uses ''' as delim
# too complex for string coloring here on SO but works:
""""//*[@id="comments_408947"]/div[2]/div[2]/div[2]"""" # works - uses """ as delim
"//*[@id=\"comments_408947\"]/div[2]/div[2]/div[2]" # works - escapes "
'//*[@id=\"comments_408947\"]/div[2]/div[2]/div[2]' # works - uses ' as delim
the same goes for your id-insertion -you can use string interpolation:
com_id = '408947'
query= f'''//*[@id="comments_{com_id}"]/div[2]/div[2]/div[2]''', com_id)
Upvotes: 1
Reputation: 2689
Use triple quotes, it will solve the issue for you or you need to escape the inner "
, otherwise it is interpreted as a string endpoint.,
'''//*[@id="comments_408947"]/div[2]/div[2]/div[2]'''
Upvotes: 1