S
S
stasykk2018-03-11 13:57:12
Python
stasykk, 2018-03-11 13:57:12

selenium. Working with tabs in Python?

Good people, don't judge cruelly, I'm just learning, despair has come and I really hope for your help, I can't learn how to switch between windows on selemiun (python).
Here is my code, the task is simple in two pages to log in to social networks:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys


class Bot:
  def __init__(self):
    self.driver = webdriver.Firefox()
    self.authorization_vk()
    self.authorization_fb()
    

  def authorization_vk(self): #АВТОРИЗАЦИЯ VK:

    
    self.driver.get('https://vk.com')
    self.driver.find_element_by_xpath('//*[@id="index_email"]').send_keys("email")
    self.driver.find_element_by_xpath('//*[@id="index_pass"]').send_keys("pass")
    self.driver.find_element_by_xpath('//*[@id="index_login_button"]').click()

    
  def authorization_fb(self): #АВТОРИЗАЦИЯ fb:

    self.driver.execute_script("window.open('https://www.facebook.com','_blank');") # Интуитивно вставила так ссылку и она хотя бы заработала, возможно есть другие варианты...
    self.driver.switch_to_window(driver.window_handles[1]) #Пробовала вычислять окна и вставлять номер id конкретного окна, что-то мелькало на экране, но результата не дало.
    self.driver.find_element_by_xpath('//*[@id="email"]').send_keys("email")
    self.driver.find_element_by_xpath('//*[@id="pass"]').send_keys("pass")
    self.driver.find_element_by_xpath('//*[@id="u_0_2"]').click()
    

    
def main():
  b = Bot()
  
  
if __name__ == '__main__':
  main()

For some reason, switch_to_window is the only working option in my case, I would gladly use element.send_keys(Keys.CONTROL + 't'), but it doesn't want to work with me...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question