I'm pretty new to the coding world, so maybe someone can help me. Might even be a straight forward problem. Currently I'm using selenium and python for a project, but I can't find anything helpful so far. Would it be possible to define the variables in a f-string as a range of numbers which are inserted in the placeholder one by one so selenium can check each f string seperately? Thats what I got so far. The range of "Wunschliste" is supposed to be between 8-18 for each of the values of "terital". First 4 then 7 and so on.
while not BuBu:
try:
Wunschliste = range(8-18)
tertial = '4,7,10'
Wunsch1 = Wu1 = browser.find_element_by_xpath(f"/html/body/div[7]/div[2]/div/table/tbody/tr{Wunschliste}]/td[{tertial}]/img")
Wu1.click()
print("Wunsch1 eingeloggt")
browser.find_element_by_class_name("pj_nicht_buchbar")
print("nope")
time.sleep(3)
except: ...
Thanks! Hope it makes sense
CodePudding user response:
You can write nested for-loops to handle each case:
for Wunschliste in range(8,19):
for tertial in [4,7,10]:
Wunsch1 = Wu1 = browser.find_element_by_xpath(f"/html/body/div[7]/div[2]/div/table/tbody/tr{Wunschliste}]/td[{tertial}]/img")
Wu1.click()
The range is from 8 to 19 because then last handled number would be 18.
CodePudding user response:
range() function returns the sequence of the given number between the given range.
Moreover, tertial
seems to be a list of integers
.
So while passing them within the xpath you have to convert them into string characters and you can use the following solution:
for Wunschliste in range(8,19):
for tertial in [4,7,10]:
Wu1 = browser.find_element_by_xpath(f"/html/body/div[7]/div[2]/div/table/tbody/tr{str(Wunschliste)}]/td[{str(tertial)}]/img")
Wu1.click()
print("Wunsch1 eingeloggt")