简体   繁体   中英

Python Selenium script does not run properly in Headless but works without Headless

My current task was to check the call center phone lines status using Python and Selenium and will send a screenshot through Slack if there is an error in the phone lines.

The program works as follows:

  1. Opens Login Page and logs in to the system
  2. Opens PhoneStatus page and loops through the table to check each and every phone line status
  3. Takes screenshot and sends message to Slack if a phone line is not working

The code :

def open_browser(url):

    # Current Time
    curr_time = datetime.now().strftime("%H%M%S")

    # Current Date
    curr_date = datetime.today().strftime('%d%m%Y')

    # Counters
    offcount = 0
    oncount = 0
    offlinecount = 0

    # Options for chrome to run Headless (No Browser will popup)
    browser_options = webdriver.ChromeOptions()
    # browser_options.add_argument('--headless')
    browser_options.add_argument('window-size=1980x1080')

    browser = webdriver.Chrome(
        executable_path='driverlocation', 
        options=browser_options)
    browser.get(url)

    browser.implicitly_wait(10)

    # Enter username
    username = browser.find_element_by_xpath("//input[@name='username'][@id='loginname']")
    username.send_keys('username')

    # Enter password
    password = browser.find_element_by_xpath("//input[@name='password'][@id='loginpass']")
    password.send_keys('password')

    # Click login
    login = browser.find_element_by_xpath("//input[@id='login_button'][@value='Login']")
    login.send_keys(Keys.ENTER)

    # Wait for web browser to load all components
    browser.implicitly_wait(10)

    # Opens new window
    browser.get('http://link/to/phoneStatus.htm')

    # Wait for web browser to load components
    browser.implicitly_wait(10)

    # Loop through table
    table = browser.find_element_by_css_selector('table.tblcontext tbody')
    for row in table.find_elements_by_css_selector('tr'):
        for data in row.find_elements_by_css_selector('td'):
            if data.text == 'OffHook':
                offcount += 1
            elif data.text == 'OnHook':
                oncount += 1
            elif data.text == 'Offline':
                offlinecount += 1

    print('Offline Count: ' + str(offlinecount))
    print('OnHook Count: ' + str(oncount))
    print('OffHook Count: '+ str(offcount))

    # Sends message to Slack if count is more than 0
    if offlinecount > 0:
        image_name = curr_date+'_'+curr_time+'.png'
        browser.save_screenshot('C:\\PythonProjects\\PhoneCheckAutomation\\'+image_name)
        # message.send_message(image_name)

    browser.quit()

The code works perfectly fine but once the line browser_options.add_argument('--headless') is uncommented, The code wont even work in the Login Page let alone the second page.

I have tried setting the window size based on some answers I saw but still does not work.

Anything else I could try?

# Options for chrome to run Headless (No Browser will popup)
browser_options = webdriver.ChromeOptions()
# browser_options.add_argument('--headless')

browser = webdriver.Chrome(
    executable_path='driverlocation', 
    options=browser_options)
browser.set_window_size(1920, 1080)
browser.get(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM