我正在尝试创建脚本以登录到USPS网站以从Informed Delivery获取传入软件包的列表。
我尝试了两种方法:
请求
我捕获了登录请求,并将其导入Postman。当我发送请求时,我收到错误:
{
"actionErrors": [
"We have encountered an error. Please refresh the page and try again."
],
"actionMessages": [],
"fieldErrors": {}
}
在请求正文中,它发送一个令牌值(来自登录表单)。请求标头还发送一些以x-jfuguzwb-
开头的标头。这些看起来是具有不同值的标记。
硒
即使使用无头浏览器也不起作用。
LOGIN_URL = "https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/"
driver.get(LOGIN_URL)
username = driver.find_element_by_name('username')
username.send_keys(USERNAME)
password = driver.find_element_by_name('password')
password.send_keys(PASSWORD)
driver.find_element_by_id('btn-submit').click()
显示一条错误消息:“很抱歉,您登录时遇到了麻烦。”
[有一个名为myusps的Python模块,但是已经有几年没有更新了。
是否有关于如何实现此目标的建议?
有关您的用例和错误的更多信息我们很抱歉您登录时遇到的问题您所看到的将有助于我们以更好的方式调试问题。但是,我能够将字符序列发送到username和password字段,并使用click()
诱导Selenium为[WebDriverWait,在Sign In]]按钮上调用element_to_be_clickable()
。 C0],则可以使用以下Locator Strategies之一:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("Bijan")
driver.find_element_by_css_selector("input#password").send_keys("Bijan")
driver.find_element_by_css_selector("button#btn-submit").click()
使用xpath:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='username']"))).send_keys("Bijan")
driver.find_element_by_xpath("//input[@id='password']").send_keys("Bijan")
driver.find_element_by_xpath("//button[@id='btn-submit']").click()
注
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照: