Home > Back-end >  integer variable on xpath element python
integer variable on xpath element python

Time:09-21

I'm trying to scrape a html table that every 30 minutes is added a new row underneath. So I need add on the xpath element a variable integer to the value that will be incremented every 30 minutes. My code now is like this:

lastline = driver.find_element_by_xpath('//table[@width="517"]/tbody/tr[8]/td[8]')

I try simple concatenation did didn't work because I have double quotation"" inside the single quotation''. So I decided put the table width value in a variable as well to then work on the tr value later

 tablewidth = 517
 lastline = driver.find_element_by_xpath("/table[@width=' "   table   "/tbody/tr[8]/td[8']")

then I would be able to use the simple concatenation like mentioned a couple times here in slack. But then I started to face another issue, seems that I can't concatenate a string with a integer variable, seems to be a very simple thing to solve but i'm stuck. Can someone help me?

CodePudding user response:

You need to convert tablewidth into a string with str(tablewidth) as such:

tablewidth = 517
lastline = driver.find_element_by_xpath("/table[@width=\""   str(tablewidth)   "\"]/tbody/tr[8]/td[8]")

I cleaned up the code a little bit as well because I think your string was incorrect. It now looks like this:

print("/table[@width=\""   str(tablewidth)   "\"]/tbody/tr[8]/td[8]")
/table[@width="517"]/tbody/tr[8]/td[8]
  • Related