1

Does driver.switch_to.window Function work with Javascript Pages for Selenium Python?

The question relates to whether driver.switch_to.window would work with JS Pages. It doesnt as per my recent finding.

I recently discovered in my project that it doesnt work.I used the work around of using the back button in browser

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import pdb
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
chrome_path=r'C:\webdrivers\chromedriver.exe'
driver=webdriver.Chrome(chrome_path)
url='https://www.google.com/'
URL_List=['1line williams']


for i in range(0,2):
    driver.get(url)
    SearchPage = driver.window_handles[0]
    searchbar=driver.find_element_by_name('q')
    time.sleep(2)
    searchbar.send_keys(URL_List[i])
    searchbar.send_keys(Keys.ENTER)



    if i==2:
       PipelineID=1
       SelectLink=driver.find_element_by_xpath('//*[@id="rso"]/div[1]/div/div/div/div/div[1]/a/h3').click() 

       time.sleep(2)
       Links=driver.find_elements_by_class_name("links")
       aElements = driver.find_elements_by_tag_name("a")
       for name in aElements:

           window_after=driver.window_handles[0]
           time.sleep(5)
           SelectPipeline=driver.find_element_by_xpath('//*[@id="splashContent_pipelines"]/div['+str(PipelineID)+']/p[2]/a[1]').click()
           time.sleep(2)
           Location=driver.find_element_by_xpath('//*[@id="navmenu-v"]/li[4]/a').click()
           time.sleep(2)
           File=driver.find_element_by_xpath('//*[@id="navmenu-v"]/li[4]/ul/li/a').click()
           time.sleep(15)
           document=driver.find_element_by_tag_name('iframe')
           driver.switch_to.frame(document)
           time.sleep(2)
           DownloadFile=driver.find_element_by_xpath('//*[@id="list:downloadBtn"]').click()
           time.sleep(2)
           driver.execute_script("window.history.go(-2)")
           PipelineID=PipelineID+1

           time.sleep(3)
           if PipelineID>3:
              driver.quit()
              break

This is the code Im using. Im using Selenium to go to google use the keyword and download data in Location column. Here I tried using driver.switch_to.window instead of execute script cmd at last(browser back button)but it dint work

So once again I would like to know if driver.switch_to.window works on JS Pages or not.

Akshay
  • 81
  • 5
  • @DebanjanB : Can u read the question before marking? The The question relates to whether driver.switch_to.window would work with JS Pages. It doesnt deal with how to switch the window in Selenium Python – Akshay Jul 08 '19 at 15:52
  • 1
    can you define "Javascript Pages"? – pcalkins Jul 08 '19 at 18:24
  • @Akshay What exactly do you mean by JS Pages? Can you update the question with your code trials? – undetected Selenium Jul 08 '19 at 18:50
  • @DebanjanB I have edited the post .Kindly have a look and let me know – Akshay Jul 09 '19 at 04:48
  • @pcalkins.I have edited the post .Kindly have a look and let me know – Akshay Jul 09 '19 at 04:48
  • Do you understand what `time.sleep(5)` is all about? What are you trying to do through `driver.execute_script("window.history.go(-2)")`? – undetected Selenium Jul 09 '19 at 06:51
  • Yes. Im trying to go back to previous window and click on another (which will be Pine and then Gul Transmission) to click on the links and donwload. I tried waiting for time.sleep and tried removing time.sleep.But it doesnt work still. Hence I used driver.execute_script("window.history.go(-2)")instead of driver.switch to function – Akshay Jul 09 '19 at 15:46

0 Answers0