提问人:gourav 提问时间:8/8/2023 最后编辑:gourav 更新时间:8/9/2023 访问量:50
如何使用 python 版本 2.7 截取任何 URL 的屏幕截图
How to take screenshot of any URL using python version 2.7
问:
我需要使用 python-2.x 截取 Power BI 报表的屏幕截图,我想传递 Power BI URL。该 URL 将具有这样的流程 -> 电子邮件 -> 提交,然后密码 -> 登录 然后保持登录 -> 是的,我已经尝试了以下代码。我必须在 power automate 中运行此脚本,而 power automate 目前仅支持 python2。
import datetime
import time
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
def login_powerbi(username, password):
# Set up the ChromeDriver path in the PATH environment variable
chromedriver_path = 'C:/Users/Desktop/Screenshot/chrome_driver/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver_path)
# Set up the webdriver
# driver = webdriver.Chrome()
actionChains = ActionChains(driver)
try:
# Open the Power BI report URL (login page)
driver.get("https://app.powerbi.com/")
# Wait for the login page to load
time.sleep(5)
wait = WebDriverWait(driver, 60)
email_field = wait.until(EC.presence_of_element_located((By.ID, "email")))
actionChains.click(email_field).send_keys(username).perform()
driver.find_element(By.CSS_SELECTOR, "[id=submitBtn]").click()
time.sleep(5)
# Wait for the password field to load and then enter the password
password_field = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[type=password]")))
actionChains.click(password_field).send_keys(password).perform()
# Submit the password form
driver.find_element(By.CSS_SELECTOR, "[id=idSIButton9]").click()
# Wait for the "Stay signed in?" prompt to load and then click "Yes"
time.sleep(3)
stay_signed_in_prompt = wait.until(EC.presence_of_element_located((By.ID, "idSIButton9"))) # idSIButton9
stay_signed_in_prompt.click()
# Wait for the Power BI report to load after authentication
driver.implicitly_wait(30)
# Add a delay before taking the screenshot to ensure the report is fully loaded
time.sleep(10)
# Take the screenshot
now = datetime.datetime.now()
file_name = 'powerBI_{}.png'.format(now.strftime("%Y-%m-%d_%H-%M-%S"))
output_path = "C:/Users/Desktop/Screenshot/output/"
screenshot_output_file = os.path.join(output_path, file_name)
screenshot_BI = driver.save_screenshot(screenshot_output_file)
print(screenshot_BI)
print("Screenshot saved as {}".format(screenshot_output_file))
except StaleElementReferenceException as e:
# Re-locate the element and retry the action
print("Stale element reference, retrying...")
login_powerbi(username, password)
except Exception as e:
print("Error during login and screenshot capture: {}".format(e))
finally:
# Close the browser
driver.quit()
if __name__ == "__main__":
# Replace 'your_username' and 'your_password' with your actual Power BI login credentials
login_powerbi("[email protected]", "password")
上面的脚本从这一行 -> actionChains.click(password_field).send_keys(password).perform() 失败,出现此错误 -> 消息:过时元素引用:找不到过时元素
答:
0赞
gourav
8/9/2023
#1
此行生成 -> StaleElementReferenceException
actionChains.click(password_field).send_keys(password).perform()
为了解决此问题,在字段定位器本身中发送密钥
password_field = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[type=password]"))).send_keys(password)
评论