我正在尝试使用 python playwright 编写脚本以登录网站,这是我的代码:
import json
import sys
import bs4
import urllib.parse
link = 'https://secure.libertycable.com/login.php'
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
#browser = p.chromium.launch()
context = browser.new_context(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36')
page = context.new_page()
page.goto(link)
try:
try:
page.click('#close')
except Exception as e:
page.wait_for_timeout(1000)
page.type('#user input[name="email"]', "xxxxx")
page.type('#myInput input[name="password"]', "xxxxxxxxxxxx")
page.close()
context.close()
browser.close()
except Exception as e:
print("Error in playwright script.")
page.close()
context.close()
browser.close()
这是页面源代码的一部分:
<form class="form-group" id="login_form2" name="login_form2" method="post" action="do_login.php">\n <input type="hidden" name="go_url" value="">\n <div style="width:50%;">\n <label>Email Address*:</label><br>\n <input class="form-control" type="email" name="email" id="user" size="25" value="">\n <label>Password*:</label><br><input class="form-control" type="password" name="password" id="myInput" size="25" value="">\n <span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password" title="View Password" onclick="passwordToggle()"></span>\n </div>\n <br clear="all">\n <div class="btn btn-green" onclick="document.getElementById(\'login_form2\').submit();"><i class="fa fa-sign-in fa-fw"></i> Login</div>\n <br><span class="text-muted">*Please Note: Email address and password are case sensitive!</span>\n </form>\n <br>\n <p><a href="reset-password.php" class="blueText">Forgot Password?</a></p>\n
脚本转到页面,关闭具有 id #close 的弹出窗口,并且工作正常,但是当需要填写输入字段时,脚本什么也不做,有什么帮助吗?