Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

python - How to close a browser tab with Selenium at a given time

I have been working on this script that automatically joins google meets. It logs in to gmail and then goes to the meeting automatically if it is the time for meeting. But, now I am having problems with leaving the meeting after certain time. I want to just close a browser tab, thus the meeting. Then continue checking for the next meeting. I think the last while loop that is intended to close the chome tab after the meeting is done does not run at all. I have tried replacing it with print statements to see if it is executed, but it does not. I do not know why not tho.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import datetime
import time
import signal

now = datetime.datetime.now()
current_time = now.strftime("%H:%M / %A")

justtime = now.strftime("%H:%M")
print (current_time)


def Glogin(mail_address, password):
    #os.system("obs --startvirtualcam &")
    # Login Page 
    driver.get( 
        'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/&ec=GAZAAQ') 

    # input Gmail 
    driver.find_element_by_id("identifierId").send_keys(mail_address) 
    driver.find_element_by_id("identifierNext").click() 
    driver.implicitly_wait(10) 

    # input Password 
    driver.find_element_by_xpath( 
        '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(password) 
    driver.implicitly_wait(10) 
    driver.find_element_by_id("passwordNext").click() 
    driver.implicitly_wait(10) 

    # go to google home page 
    driver.get('https://google.com/') 
    driver.implicitly_wait(100)
    driver.get(sub)
    # turn off Microphone 
    time.sleep(1)
    #driver.find_elements_by_class_name("JRY2Pb")[0].click()
    driver.find_elements_by_class_name("JRY2Pb")[0].click()
    # switch camera
    time.sleep(2)
    for x in driver.find_elements_by_class_name("JRtysb"):
        x.click()
    time.sleep(2)
    for a in driver.find_elements_by_class_name("FwR7Pc"):
        a.click()
    time.sleep(2)
    for b in driver.find_elements_by_class_name("XhPA0b"):
        b.click()
    time.sleep(2)
    driver.find_element_by_tag_name('body').send_keys(Keys.TAB + Keys.TAB + Keys.ARROW_DOWN + Keys.ENTER)
    time.sleep(1)
    webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
    time.sleep(2)
    # Join meet 
    time.sleep(1) 
    driver.implicitly_wait(2000) 
    driver.find_element_by_css_selector( 
        'div.uArJ5e.UQuaGc.Y5sE8d.uyXBBb.xKiqt').click()
    
# assign email id and password
mail_address = 'email'
password = 'password'

# create chrome instamce 
opt = Options() 
opt.add_argument('--disable-blink-features=AutomationControlled') 
opt.add_argument('--start-maximized')
opt.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.media_stream_mic": 1, 
    "profile.default_content_setting_values.media_stream_camera": 1, 
    "profile.default_content_setting_values.geolocation": 0, 
    "profile.default_content_setting_values.notifications": 1
})

while True:

    if current_time == "05:00 / Wednesday":
        
        sub = "link"
        driver = webdriver.Chrome(options=opt, executable_path=r'/usr/bin/chromedriver') 
        Glogin(mail_address, password)
        break
    
while True:
    if current_time == "05:01 / Wednesday":
        driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
        break
            

        

        
question from:https://stackoverflow.com/questions/65911546/how-to-close-a-browser-tab-with-selenium-at-a-given-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the last while loop isn't running, it's because the previous while True loop never broke.

I suspect it has something to do with your condition current_time == "05:00 / Wednesday", which means current_time is never being set equal to "05:00 / Wednesday".

Based on the limited context, I can only suggest two things.

  • First off, don't use while True loops with only one if statement inside, use the if-condition as your while loop condition.
  • Secondly, you may want to reset current_time in your loop. Modify your loop to look something like this:
run_loop = True # will be false when we want our loop to quit
while run_loop:

    if current_time == "05:00 / Wednesday":
        sub = "link"
        driver = webdriver.Chrome(options=opt, executable_path=r'/usr/bin/chromedriver') 
        Glogin(mail_address, password)
        run_loop = False #break us out of the loop
    else: #we keep trying to get the time to see if it's 05:00 yet
       now = datetime.datetime.now()
       current_time = now.strftime("%H:%M / %A")

The above code will continuously check if the time meets your conditions, and then exit appropriately.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...