I have a problem with a python for loop where is executing only the first time and on the second loop the first part of the code is skipped. I know is sound confusing but when I describe the problem you'll see what I mean. I'm not that advanced programmer but I think I know how to write a simple for loop, and the one that I have by all meaning should work, but it is not. So, what the code does is:
Open a page on a social network where all the followers are.
- Scroll pgdown so that the first 12 followers are shown.
- find the user to send message and open in to new tab (that will be the first user, and open the user profile in new tab)
- switch to the new opened TAB where the user that need to post is loaded
- wait for the post icon to show and click it etc... so at the end, on the message is sent etc...
once the message is send:
- close the current tab
- switch back to the very first tab, where all the 12 profiles are loaded from the for loop beginning and should start the same sequence, only this time need to open the second follower.
However once the second iteration start I get error "index out of range for driver.switch_to.window(driver.window_handles[1])" which tells me that once the for loop start for second time it is skipping the part where it gets the xpath for the second user (point number 2 from the points above), and instead goes directly to the new opened TAB, which ofcourse does not exist because it is not opened. I'm also pretty sure that the xpath is correct because if it is incorrect the error will be something like, "xpath does not exist or can not be found..." but I dont get any of those, just as I said, like that part from the code is not executed the second time.
p.s. I use chrome if anything helps
Any help will be appreciated.
# 1. scroll down so that all 12 followers are shown
driver.execute_script("window.scrollTo(0, 450)")
for n in range(1, 13):
# 2. find the user to send message and open in to new tab
find_user = '//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/main/div/div/div/div/div[3]/div/div[2]/div/div/div[2]/div[{}]/div/div/div[1]/div/span/a/div/p'.format(
n)
user_to_click = driver.find_element(By.XPATH, find_user)
ActionChains(driver) \
.key_down(Keys.CONTROL) \
.click(user_to_click) \
.key_up(Keys.CONTROL) \
.perform()
# load the message
for idxi, message in enumerate(messages):
new_message = message["message_to_post"]
sleep(2)
# 3. switch to TAB where the user that need to post is open
driver.switch_to.window(driver.window_handles[1])
# 4. wait for the post icon to show and click it
driver.implicitly_wait(20)
if fourth_button():
driver.find_element(By.XPATH,
'//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/main/div/div/div/div/div[1]/div[2]/div/div[2]/div/button[2]').click()
else:
driver.find_element(By.XPATH,
'//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/main/div/div/div/div/div[1]/div[2]/div/div[2]/div/button[1]').click()
# wait for the profile pic to be shown in the messenger, which is an indicator that the message can be send
driver.implicitly_wait(30)
try:
driver.find_element(By.XPATH,
'//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/div/div/div/div[1]/div/div[1]/a/div/img')
except NoSuchElementException:
pass
# once the profile pic is loaded it paste the message in the send message field
try:
driver.find_element(By.XPATH,
'//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/div[2]/div/div/div[2]/div/div[2]/div/div[2]/div[2]/div[2]/div/div/div/textarea').send_keys(
new_message)
except NoSuchElementException:
pass
# click on the send message button and send the message
driver.implicitly_wait(5)
try:
driver.find_element(By.XPATH,
'//*[@id="soapbox"]/div[1]/div/div[3]/div[1]/div/div[2]/div/div/div[2]/div/div[2]/div/div[2]/div[2]/div[3]/button').click()
except NoSuchElementException:
pass
sleep(3)
# close the current tab
driver.close()
# switch to the first main tab where all the profiles are loaded
driver.switch_to.window(driver.window_handles[0])
sleep(2)