提问人:Grim Lock 提问时间:11/11/2023 最后编辑:ggorlenGrim Lock 更新时间:11/11/2023 访问量:28
使用 Playwright 在 Python 中从学校日历 URL 添加 iCal 事件
Adding iCal Events from School Calendar URL in Python using Playwright
问:
我目前正在使用 Playwright 编写一个 Python 脚本,以自动执行将 iCal 链接添加到 Google 日历的过程。该脚本成功浏览了身份验证过程,输入了登录凭据,但在尝试在 Google 日历的“按网址添加”页面上输入网址时遇到了问题。它必须在 headless = True 中
from playwright.sync_api import sync_playwright
import time
from playwright.sync_api import Page
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=True, slow_mo=5000)
context = browser.new_context()
page = context.new_page()
page.goto("https://accounts.google.com/v3/signin/identifier?dsh=S1611624178:1665765818620%20318&continue=https://calendar.google.com/calendar/r&followup=https://calendar.google.com/calendar/r&osid=1&passive=1209600&service=cl&flowName=GlifWebSignIn&flowEntry=ServiceLogin&ifkv=AQDHYWrL2lk0_Bcr1n1Y-f-i1sNZRKJK8CNisliX9rpozkqKhY2Jby8gsVZ_wDz_oHqiWmN6uZ6s6g&ec=wgc-calendar-globalnav-signin")
userLogin = "username "
userPass = "password"
page.fill('input[type="email"]', userLogin)
page.wait_for_selector('#identifierNext >> button', state="visible")
page.wait_for_timeout(2000)
page.click('#identifierNext >> button')
page.evaluate('''() => {
const passwordInput = document.querySelector('input[type="password"]');
passwordInput.style.display = 'inline'; // or 'block' or 'inline-block'
}''')
password_input = page.wait_for_selector('input[type="password"]', state="visible")
password_input.fill(userPass)
page.click('button >> nth=1')
page.goto('https://calendar.google.com/calendar/u/0/r/settings/addbyurl')
url_to_inject = 'https://brightspace.uri.edu/d2l/le/calendar/feed/user/feed.ics?token=althvrqormrwijfhdbe8'
page.evaluate('''(url) => {
const inputField = document.querySelector('input[jsname="YPqjbf"]');
if (inputField) {
// Click on the input field
inputField.click();
inputField.focus();
inputField.click();
// Set the value
inputField.value = url;
}
}''', url_to_inject)
# Check if the attribute is set on the input field
attribute_value = page.evaluate('(url) => document.querySelector(\'input[jsname="YPqjbf"]\').getAttribute(\'data-injected\')', url_to_inject)
if attribute_value == 'true':
print("Script injection successful!")
else:
print("Script injection failed!")
我得到错误:
playwright._impl._api_types.Error: TypeError: Cannot read properties of null (reading 'style')
at eval (eval at evaluate (:208:30), <anonymous>:3:23)
at UtilityScript.evaluate (<anonymous>:215:19)
at UtilityScript.<anonymous> (<anonymous>:1:44)
该输入字段的 HTML 代码段:
<input type="text" value="" id="c67" jsname="YPqjbf" class="VfPpkd-fmcmS-wGMbrd " jsaction="focus:AHmuwe;blur:O22p3e;input:YPqjbf; mousedown:UX7yZ; mouseup:lbsD7e; pointerdown:QJflP; pointerup:HxTfMe; touchstart:p6p2H; touchend:yfqBxc;" aria-controls="c68" aria-describedby="c68" aria-labelledby="c64" maxlength="1024" autocomplete="off">
我尝试了很多方法,我了解它的动态,我希望它输入它,然后我可以在它执行所有其他操作后获取嵌入式代码:
#Checkbox after url input
# page.evaluate('''(selector) => {
# const checkbox = document.querySelector(selector);
# if (checkbox) {
# checkbox.checked = true;
# checkbox.dispatchEvent(new Event('change'));
# }
# }''', 'input[jsname="YPqjbf"][type="checkbox"]')
#Add button clicked
# page.evaluate('''(selector) => {
# const button = document.querySelector(selector);
# if (button) {
# button.click();
# }
# }''', 'button[jsname="V67aGc"]')
# page.get_by_text("All Courses - University of Rhode Island").click()
# page.get_by_role("treeitem", name="Integrate calendar").click()
#embedded code link
# dynamic_selector = '[jsname="YPqjbf"]'
# page.wait_for_selector(dynamic_selector, timeout=20000)
# embed_code_element = page.locator(dynamic_selector).nth(4)
# print(embed_code_element)
# element_html = str(embed_code_element.input_value())
# page.screenshot(path='screenshot.png')
# print(element_html)
答: 暂无答案
评论