Python: open two different tabs via selenium
<p>i try to open two different tabs in browser through Selenium. 
But when i finished query in first tab and switched to second tab, my next query perform in first tab again. What do i have to change for perform two queries in different tabs (not in one tab like now).</p>

<pre><code>    &lt;!-- language: python3 --&gt;
import time,os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = "/home/andrew/ÐÐ°Ð³ÑÑÐ·ÐºÐ¸/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
for elem in range(0,3):

    driver.find_element_by_tag_name("body").send_keys(
    Keys.CONTROL + "t")

    driver.get("http://google.com")

    time.sleep(3)
    # first tab
    search = driver.find_element_by_name('q')
    search.send_keys('andrew sotnikov site:progreso.com.ua')
    search.send_keys(
        Keys.RETURN)  # hit return after you enter search text
    time.sleep(5)  

    time.sleep(3)
    # second tab
    driver.find_element_by_tag_name("body").send_keys(
    Keys.CONTROL + "t")

    driver.get("http://google.com")
    time.sleep(2)
    search = driver.find_element_by_name('q')
    search.send_keys('andrew sotnikov site:progreso.com.ua')
    search.send_keys(
        Keys.RETURN)  # hit return after you enter search text
    time.sleep(5)  


    driver.find_element_by_tag_name('body').send_keys(
        Keys.CONTROL + 'w')
</code></pre>

<p>The best solution for me is using window_handles, as <strong>Saurabh Gaur</strong> advised me.
But before switching beetween tabs i should declare all my tabs. Just  after that i can switch tabs like any iterable object.
my solution is below:</p>

<pre><code>for elem in range(0,3):
    driver.find_element_by_tag_name("body").send_keys(
    Keys.CONTROL + "t")

for handle in driver.window_handles:

    driver.get("http://google.com")
    driver.switch_to_window(driver.handle)
    time.sleep(2)
    search = driver.find_element_by_name('q')
    search.send_keys('andrew sotnikov site:progreso.com.ua')
    search.send_keys(
        Keys.RETURN)  # hit return after you enter search text
    time.sleep(5)  
</code></pre>

python selenium