1

I'm using python 2.7. I've looked at these questions for help with opening links in new tabs, and that seems to be working initially. However, I run into an issue once I try to close a tab. Here is my code:

actions.key_down(Keys.COMMAND).click(people[x]).key_up(Keys.COMMAND).perform()
driver.switch_to.window(driver.window_handles[-1]) # now at user page
# interact with user page
driver.close()
driver.switch_to.window(driver.window_handles[0])

This is within a for loop going through each element in the people array, where people is a list of web elements that correspond to individual Twitter profiles that a user is following. On the first run, it correctly clicks on the first person's profile in a new tab, switches to the tab that was created, interacts with it, closes it, and refocuses on the original tab (with the list of people). However, on the second iteration, when it "clicks" on the second person, it actually opens a tab for the 1st person and 2nd person, which is not correct. I believe it should only be clicking on one thing, which would now be the 2nd person's profile, since I only have one click action and it is on people[x].

What could be going wrong?

1 Answers1

0

Try to iterate this part:

driver.switch_to.window(driver.window_handles[-1])

So your second user be like:

driver.switch_to.window(driver.window_handles[-2])

You would need to declare global variable and change the value at the end of for loop.

Hope it helps.

Slav Kurochkin
  • 444
  • 3
  • 9
  • while this does technically work in that now i am interacting with the correct page, it still does not address the issue that multiple tabs are being opened on each click. I would really like to find a way where I open only the tab corresponding to the user I want to click on, because otherwise I am left with many open tabs, even if I don't use them. Given that I will be doing this near 100 times, I am not sure if Chrome can handle that many open tabs at a time, and I want to avoid having to close n-1 tabs each time as I believe this would be very inefficient. – R Balasubramanian Jul 28 '17 at 18:13