Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
如何使用selenium python打印PDF环境_Python_Selenium_Printing_Landscape - Fatal编程技术网

如何使用selenium python打印PDF环境

如何使用selenium python打印PDF环境,python,selenium,printing,landscape,Python,Selenium,Printing,Landscape,我已经成功地使用上面的脚本打印和下载了pdf,但是我遇到了一个问题,如何在横向模式输出中使用selenium python打印/下载pdf 我尝试使用pdfkit和selenium“driver.getsource”的“fromstring”,但纵向模式效果不太好,最后我选择使用chromedriver的打印PDF,我成功地制作了一个PDF文件,我想要的效果很好,但只停留在纵向模式。即使我需要的是横向模式 from selenium import webdriver from selenium.

我已经成功地使用上面的脚本打印和下载了pdf,但是我遇到了一个问题,如何在横向模式输出中使用selenium python打印/下载pdf

我尝试使用pdfkit和selenium“driver.getsource”的“fromstring”,但纵向模式效果不太好,最后我选择使用chromedriver的打印PDF,我成功地制作了一个PDF文件,我想要的效果很好,但只停留在纵向模式。即使我需要的是横向模式

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

import time
import pdfkit
import json
import os

curr_dir = os.getcwd()
download_path = os.path.join(curr_dir, 'dl')
appState = {
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local"
        }
    ],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
}

profile = {
    'printing.print_preview_sticky_settings.appState': json.dumps(appState),
    'savefile.default_directory': download_path,
}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome("D:\\master\\chromedriver\\2.45\\chromedriver.exe", 
chrome_options=chrome_options)
URL = "taget site"


def site_login():
    driver.get(URL)
    driver.find_element_by_id('username').send_keys('username')
    driver.find_element_by_id ('password').send_keys('pass')
    driver.find_element_by_name('login').click()

def get_abc():
    driver.get('https://targetsite/abc')
    driver.find_element_by_name('year_id_combo')
    s1 = Select(driver.find_element_by_name('year_id_combo'))
    s1.select_by_visible_text('summer')

    select_box = driver.find_element_by_name('course_id')

    mk = [x for x in select_box.find_elements_by_tag_name("option")]

    course_name = mk[2].text
    course_name = course_name.replace(' ', '_')

    index_mk = course_name.index('|')
    course_name = course_name[:index_mk]

    s2 = Select(driver.find_element_by_name('course_id'))
    s2.select_by_value(mk[2].get_attribute('value'))

    s3 = Select(driver.find_element_by_name('class'))

    list_class = driver.find_element_by_name('class')
    class = [x for x in list_class.find_elements_by_tag_name("option")]

    s3.select_by_value(class[1].get_attribute('value'))

    # multiple windows handle
    main_window_handle = None
    while not main_window_handle:
        main_window_handle = driver.current_window_handle

    driver.find_element_by_xpath( '/html/body/div[3]/div[2]/div/div/div[1]/div[1]/div/div[2]/div[2]/div/input[1]').click()
    print_window_handle = None
    while not print_window_handle:
        for handle in driver.window_handles:
            if handle != main_window_handle:
               print_window_handle = handle
               break

    driver.switch_to.window(print_window_handle)
    webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()

    driver.execute_script(
        "document.title='{}';window.print();".format(course_name)
    )

def logout_system():
    driver.find_element_by_xpath('//a[normalize- 
    space(text())="Logout"]').click()


def close():
    driver.close()
    driver.quit()


site_login()
time.sleep(5)
get_abc()
time.sleep(70)
logout_system()
time.sleep(5)
close()

我希望结果以pdf横向模式打印,但我不知道如何在这种情况下使其横向显示,现在的结果只是一个纵向pdf。

您可以使用selneium的键盘操作键和python的“PyUserInput”包的组合,独立于selenium自动按键

因此,使用Selenium时,如果您使用的是Windows,则可以使用“CTRL+p”组合

然后从Selnium或PyUserInput下的“pykeyboard”,您可以自动执行“Tabbing”的按键过程,直到您看到纵向与横向下拉列表

然后,您可以使用中的箭头键,然后单击使用这些自动按键来单击“回车”或“回车”键以另存为横向视图