Home > Net >  How to call second python file and execute it with the same browser that was opened in first file
How to call second python file and execute it with the same browser that was opened in first file

Time:01-20

 //MainFile.Py            
    import time  
    from selenium import webdriver  
    from selenium.webdriver.chrome.service import Service  
    from selenium.webdriver.common.by import By  
    driver.maximize_window()  
    driver.get("https://onesys.com/")       
    time.sleep(5)  
    username = driver.find_element(By.ID, "okta-signin-username")  
    password = driver.find_element(By.ID, "okta-signin-password")  
    username.send_keys("username")  
    password.send_keys("password")  
    driver.find_element(By.ID, "okta-signin-submit").click()  
    driver.find_element(By.XPATH, '').click()     
    import Report  
    exec("Report.py")  

  //Report.py  
    from selenium import webdriver   
    from selenium.webdriver.chrome.service import Service  
    from selenium.webdriver.common.by import By  
    from selenium.webdriver.common.keys import Keys         
    s = Service(r'C:\Users\taranums\Downloads\chromedriver_win32\chromedriver') 
    driver = webdriver.Chrome(service=s)  
    driver.maximize_window()  
    driver.get("https://onesys.com/")  
    driver.find_element(By.XPATH, '').click()  
    driver.find_element(By.XPATH, '').click() 

Above code is working fine in first file but the second file only new chrome browser is opened without the website, I want first file to execute and than on same browser second file should be executed also to add Iam new to python selenium, Can anyone please help, Thanks in advance.

CodePudding user response:

You organize things into functions:

MainFile.py:

import time  
from selenium import webdriver  
from selenium.webdriver.chrome.service import Service  
from selenium.webdriver.common.by import By 
import Report  

def finder(driver):
    driver.maximize_window()  
    driver.get("https://onesys.com/")       
    time.sleep(5)  
    username = driver.find_element(By.ID, "okta-signin-username")  
    password = driver.find_element(By.ID, "okta-signin-password")  
    username.send_keys("username")  
    password.send_keys("password")  
    driver.find_element(By.ID, "okta-signin-submit").click()  
    driver.find_element(By.XPATH, '').click()     

def main():
    s = Service(r'C:\Users\taranums\Downloads\chromedriver_win32\chromedriver') 
    driver = webdriver.Chrome(service=s)  
    finder(driver)
    Report.finder(driver)

if __name__ == "__main__":
    main()

Report.py:

from selenium import webdriver   
from selenium.webdriver.chrome.service import Service  
from selenium.webdriver.common.by import By  
from selenium.webdriver.common.keys import Keys         

def finder(driver):
    driver.maximize_window()  
    driver.get("https://onesys.com/")  
    driver.find_element(By.XPATH, '').click()  
    driver.find_element(By.XPATH, '').click() 

def main():
    s = Service(r'C:\Users\taranums\Downloads\chromedriver_win32\chromedriver') 
    driver = webdriver.Chrome(service=s)  
    finder(driver)

if __name__ == "__main__":
    main()
  •  Tags:  
  • Related