Selenium ChromeDriver不与Chrome打印对话框交互

2024-10-01 15:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我是网络刮板新手,最近我尝试创建一个网络刮板来打印我的在线日历。我已经能够通过Selenium找到打印提示,但是当我试图通过编程方式单击打印时,我无法与实际的打印窗口交互取消按钮

打印提示确实需要一两秒钟才能加载,我已经让Webdriver选择打印窗口作为当前窗口(使用下面的代码完成)

whandles = driver.window_handles
print("About to switch window handle to print window, should be: " + whandles[1])
driver.switch_to.window(whandles[1])

但当我尝试时:

printBtnFinal = WebDriverWait(driver,100).until(EC.element_to_be_clickable((By.CLASS_NAME,'action-button'))).click()

什么都没有发生,程序超时(我假设是因为元素从未找到)

如何与Chrome打印窗口成功交互? 感谢您的帮助


Tags: to刮板网络driverselenium编程方式be
1条回答
网友
1楼 · 发布于 2024-10-01 15:28:05

问题解释

您不需要switch to Print Modal pop up。因为它不是new windows/tab,也不是在frame。因此,转换是罪魁祸首

解决方案

我建议您使用WebDriverWait,它是Selenium库中的显式等待

要在位于右上角的Printclick,请使用此xpathcode

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Print']/ancestor::button"))).click()

单击后,您将看到一个带有PrintCancel{}的new pop up

要在弹出窗口中单击“打印”,下面的代码应该可以帮助您克服所面临的问题

wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Print']/ancestor::button[contains(@class,'action')]"))).click()

导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

更新1:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
driver.get("https://outlook.live.com/owa/?state=1&redirectTo=aHR0cHM6Ly9vdXRsb29rLmxpdmUuY29tL2NhbGVuZGFyLw&nlp=1")
wait = WebDriverWait(driver, 20)

#User Must place Email:
USER_EMAIL = 'user name should be given here'
USER_PASSWORD = 'password should be here'

emailField = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='email']")))
emailField.clear()
emailField.send_keys(USER_EMAIL)
submitEmail= WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='submit']"))).click()
#Dismiss "It looks like this email is used with more than one account from Microsoft" screen
#personalAccount = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[id='msaTile']"))).click()

#Enter Password
passwordField = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='password']")))
passwordField.clear()
passwordField.send_keys(USER_PASSWORD)
submitPassword = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='submit']"))).click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.table-row"))).click()
time.sleep(30)

wait.until(EC.element_to_be_clickable((By.ID, "idSubmit_SAOTCC_Continue"))).click()
#Dismiss StaySignedIn
staySignedIn = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[id='idBtn_Back']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Calendar']"))).click()
printBtn1 = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Print']/ancestor::button"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Print']/ancestor::button[contains(@role,'menuitem')=false]"))).click()

相关问题 更多 >

    热门问题