Home > Back-end >  How does moving or switching between windows or frames work in Selenium
How does moving or switching between windows or frames work in Selenium

Time:01-23

I am writing a webscraping script that automatically logs into my Email account and sends a message.

I have written the code to the point where the browser clicks the "new Message" button. After that a new window (or frame?) opens where I can type in recipient address, subject and message.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

myPassword = 'xxxxxxxxxxxxxxxx'

browser = webdriver.Firefox() # Opens Firefox webbrowser
browser.get('https://protonmail.com/') # Go to protonmail website
loginButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.btn-ghost:nth-child(1)")))
loginButton.click()
usernameElem = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#username")))
usernameElem.send_keys("[email protected]")
passwordElem = browser.find_element_by_css_selector("#password")
passwordElem.send_keys(myPassword)
anmeldenButton = browser.find_element_by_css_selector(".button")
anmeldenButton.click()
newMessage = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[3]/div/div/div[1]/div[2]/button")))
newMessage.click()
addressElem = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#to-composer-1591")))
addressElem.send_keys('[email protected]')

At the first of the following lines I get a TimeoutException error:

addressElem = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#to-composer-1591")))
addressElem.send_keys('asdf')

I think the error occurs, because the browser does not know where to find the element, correct?

I checked the documentation: enter image description here

The source code of the new window or frame:

<span >Neue Nachricht</span>

enter image description here

I am not sure if I am on the right way to solve the problem. I am trying to find information in the source code, that helps the browser to find the correct element and make use of the .switch_to_window() or .switch_to_frame() methods. First, I am not sure, if it is really a problem of switching to a new window or frame (I don't really know the difference between them). Second: how can I achieve to switch to the new element so that the browser can apply the .send_keys() method?

CodePudding user response:

I looked into the HTMLDOM and could not see any iframe or windows getting opened.

The reason why you are not able to interact with to input field is cause you are using this CSS selector #to-composer-1591 the last part 1591 is dynamic in nature, what it means is that it will change on every new browser instance.

Fix: You should use input[id^='to-composer'] which is unique in entire HTMLDOM.

addressElem = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id^='to-composer']")))
addressElem.send_keys('[email protected]')
  •  Tags:  
  • Related