Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium脚本不能在Headless中正常运行,但在没有Headless的情况下可以工作_Python_Selenium_Google Chrome Headless - Fatal编程技术网

Python Selenium脚本不能在Headless中正常运行,但在没有Headless的情况下可以工作

Python Selenium脚本不能在Headless中正常运行,但在没有Headless的情况下可以工作,python,selenium,google-chrome-headless,Python,Selenium,Google Chrome Headless,我当前的任务是使用Python和Selenium检查呼叫中心电话线的状态,如果电话线出现错误,我会通过Slack发送一个屏幕截图 该计划的工作如下: 打开登录页面并登录到系统 打开PhoneStatus页面,并在表格中循环检查每个电话线的状态 拍摄屏幕截图,并在电话线不工作时向Slack发送消息 代码: def open_browser(url): # Current Time curr_time = datetime.now().strftime("%H%M%S")

我当前的任务是使用Python和Selenium检查呼叫中心电话线的状态,如果电话线出现错误,我会通过Slack发送一个屏幕截图

该计划的工作如下:

  • 打开登录页面并登录到系统
  • 打开PhoneStatus页面,并在表格中循环检查每个电话线的状态
  • 拍摄屏幕截图,并在电话线不工作时向Slack发送消息
  • 代码

    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()
    
    代码工作得非常好,但是一旦行
    浏览器选项.add\u参数('--headless')
    未注释,代码甚至在登录页面中都无法工作,更不用说第二个页面了

    我曾尝试根据我看到的一些答案设置窗口大小,但仍然不起作用


    还有什么我可以试试的吗?

    正确的语句是
    options。添加参数('headless')
    。我不知道你从哪里选的那两条领先的破折号。而且,没有这样的窗口大小。有1920x1080。我尝试在没有两个前导破折号的情况下使用,但都不起作用。我还尝试添加browser.set_window_size(19201080),但已从相关代码中删除。相同的结果,不起作用。正确的WindowsSize语句是:
    options.add_参数('window-size=1920x1080')
    我通常也会将二进制位置添加为:
    options.binary_location='/usr/bin/google chrome'
    尝试使用您的语句。遗憾的是,它仍然不起作用。
    # 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)