Home > Enterprise >  Selenium with Python: Using a loop in read text
Selenium with Python: Using a loop in read text

Time:04-27

I am trying to pass the i parameter into the xpath expression, however it seems the syntax is not correct.

i = 1

while i < 9:
   
   day1Temp = driver.find_element(By.XPATH, "//*[@id='wob_dp']/div[", i, "]/div[3]/div[1]/span[1]")
   
print(day1Temp.text)

Can anyone suggest what is wrong here?

Thanks!

CodePudding user response:

to parse i, you can use f-string like below:

i = 1

while i < 9:
    day1Temp = driver.find_element(By.XPATH, f"//*[@id='wob_dp']/div[{i}]/div[3]/div[1]/span[1]")

CodePudding user response:

This is how you can fix your code. Also don't forget to increment i

i = 1

while i < 9:   
   day1Temp = driver.find_element(By.XPATH, "//*[@id='wob_dp']/div[%d]/div[3]/div[1]/span[1]" % i)  
   print(day1Temp.text)
   i  = 1
  • Related