0

I am trying to create a script to login to USPS website to get a list of incoming packages from Informed Delivery.

I have tried two methods:

  1. Requests
  2. Selenium

Requests

I captured the Login request and imported into Postman. When I sent request, I received error:

{
    "actionErrors": [
        "We have encountered an error.  Please refresh the page and try again."
    ],
    "actionMessages": [],
    "fieldErrors": {}
}

In the request body, it sends a token value (from login form). The request headers also send a few headers starting with x-jfuguzwb-. These look to be tokens of different values.


Selenium

Even using a headless browser didn't work.

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()

displays an error saying "Our apologies that you are having issues with your login."


There was a Python Module called myusps but it has not been updated for a couple years.

Are there any suggestions as to how I can accomplish this?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 1
    Hmm. That's really interesting. I just tried it myself, and it doesn't work. I also tried it with Mozilla Firefox, time.sleep, driver.wait() to no avail. – zenalc Jun 05 '20 at 01:09
  • What's interesting is that resending the exact same request causes it to produce an error so requests is out of the question. – Bijan Jun 05 '20 at 02:55

2 Answers2

1

A bit of more information about your usecase and the error Our apologies that you are having issues with your login which you are seeing would have helped us to debug the issue in a better way. However, I was able to send a character sequence to both the username and password field and invoke click() on the Sign In button using Selenium inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using :

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    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()
    
  • Using :

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    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()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

usps

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The problem is not waiting for the element to be callable. I ran two versions of this. One auto-filled in the fields (even used `ActionChains` and `send_keys(Keys.TAB)`/`send_keys(Keys.RETURN)` to simulate going through the fields naturally. The other version opened the page in via webdriver as well but I entered the credentials manually. The first method returned error "Our apologies that you are having issues with your login". The second worked fine. – Bijan Jun 05 '20 at 17:55
  • I compared the POST requests in both and the only difference besides the hidden token in form, was a difference in the Headers: `x-jfuguzwb-f`,`x-jfuguzwb-b`,`x-jfuguzwb-a`, – Bijan Jun 05 '20 at 18:00
  • @Bijan About you first comment, the waiter wasn't about _waiting for the element to be callable_ but for waiting for the element to be **`clickable`**. Using `ActionChains` would have been an overkill. About your second comment, I would avoid any comparison between _Selenium_ and _Requests_ as that would be a bigger topic much beyond the scope of this question. Did you happen to execute the codeblocks from my answer? Can you update me the status please? – undetected Selenium Jun 19 '20 at 14:10
  • Your codeblock also returned the same login error (when they detect automation). I actually emailed you about this a couple weeks ago with extreme level of detail. – Bijan Jun 19 '20 at 18:07
  • @Bijan Not sure what you meant by `"they detect automation"`. I have run through the home page HTML of the application. The minor hurdles can be easily overcome using the code in my answer. – undetected Selenium Jun 19 '20 at 19:12
  • I changed username/password to mine and after Sign In is clicked, it returns an error `Our apologies that you are having issues with your login. We recommend that you restart the current browser and try logging in again........ We do not recognize your username and/or password. Please try again.`. If I login with an actual wrong password, the error is just `We do not recognize your username and/or password. Please try again.`. – Bijan Jun 19 '20 at 22:17
  • Here are my test cases (FYI I am using your code as the basis for all this) 1) Use Python/Selenium to open USPS page, fill credentials, and push submit: Returns error 2) Use Python/Selenium to open USPS page, fill credentials, but I manually push submit: Returns error 3) Use Python/Selenium to open USPS page, I manually fill credentials, click Sign In via Python: Returns error 4) Use Python/Selenium to open USPS page, I manually fill credentials and press Sign In: Logs in perfectly. – Bijan Jun 19 '20 at 22:25
  • @Bijan they may be using Javascript experimental technology with `navigator.webdriver` which can detect if the browser is automated (`navigator.webdriver` is set to true on selenium) and block it. – Anna Nevison Jun 21 '20 at 21:23
  • @AnnaNevison The solution you commented under already adds the experimental options to disable `enable-automation` and turn off `useAutomationExtension` – Bijan Jun 22 '20 at 04:42
  • @Bijan that doesn't always work because there is a flag in the chrome driver binary that websites can detect and block – Anna Nevison Jun 22 '20 at 14:42
  • I even added code to change `Page.addScriptToEvaluateOnNewDocument` to change navigator.webdriver to undefined and still same thing – Bijan Jun 22 '20 at 15:48
  • @AnnaNevison If you look at the question body OP's main [**bounty**](https://stackoverflow.com/help/bounty) concern was **to create a script to login to USPS website** and as far as _Selenium_ is concerned I think the steps till invoking a successful `click()` on the **Sign In** button should suffice. Now, if _ChromeDriver_ driven _Chrome_ browser is getting detected, that's a all together a different issue and must be addressed through a different discussion. I will be happy to write another story on _Chrome browser getting detected_ :) – undetected Selenium Jun 22 '20 at 18:49
  • @DebanjanB but the web driver is being imported by `selenium` so it does fall within the bounds of the question he's asking- and if the script does not work because `selenium` is importing a web driver that has something that can be detected then bounty concern is not met. – Anna Nevison Jun 22 '20 at 20:33
  • @AnnaNevison I'm still not sure how you concluded that the _script does not work_ and _bounty concern is not met_. I'm sure if you have a deeper look into the question the original concern is about **to create a script to login to USPS website** which is a legitimate bounty question and definitely not about the error post the invocation of `click()` on the _Sign In_ button which won't be reproducible ever unless you have a valid set of credentials which OP didn't provide within the question. Question for you: 1) Were you able to reproduce the error _Our apologies that you are having issues..._ – undetected Selenium Jun 22 '20 at 20:47
1

This answer below has helped me solve automation issues with site logins that shall go unnamed. I recommend taking a look at the user @colossatr0n answer.

You can use vim, or as @Vic Seedoubleyew has pointed out in the answer by @Erti-Chris Eelmaa, perl, to replace the cdc_ variable in chromedriver(See post by @Erti-Chris Eelmaa to learn more about that variable). Using vim or perl prevents you from having to recompile source code or use a hex-editor. Make sure to make a copy of the original chromedriver before attempting to edit it. Also, the methods below were tested on chromedriver version 2.41.578706.

Can a website detect when you are using selenium with chromedriver?