5

First time trying the forum, but today having trouble running selenium scripts with Salesforce. Everytime Im running a script locally I get a two step verification screen. I then have to input the code I receive, and then progress with my tests. Although the problem is, Everytime I run selenium scripts same accounts, even though I have done the verification and it then repeats the same process. When going to sign in This had only started recently as of yesterday as we had tests running on jenkins everyday.

Just wondering If anyone ever encountered this, or is it a problem with my set up.

Thanks very much

2
  • just a guess: does selenium delete all it's cookies ? Jan 28, 2016 at 9:50
  • I also faced same issue after January 27 release, right now this occurs in sandbox environment. Anyone can easily reproduce by logging in at chrome incognito-->Enter code-->Close browser and re login (It will again ask for code). Please suggest any way to disable this feature.
    – user28277
    Jan 29, 2016 at 13:15

6 Answers 6

5

We had this problem as well starting on 1/27 and we decided to add the IP address that Selenium uses for running our automated tests. You can find that IP address by going to (via Setup- Manage Users) the login/username profile in Salesforce that you use for running your Selenium Automated tests then look at the Login History section for that user.

2
  • Ye I ended up just doing this and worked then straight away for me and my tests :) Feb 12, 2016 at 17:01
  • 1
    Fun fact, you can also just google "my ip"
    – Adrian Larson
    Feb 18, 2016 at 18:16
2

I am having the very same problem with our Salesforce test automation and just using a browser profile that has the Salesforce cookies does not work for me - or more precisely - it works only the first time. It seems that even when using a custom Firefox profile Selenium runs the tests in a copy of that profile. Any changes in the cookies that happen during the test execution are not persisted for the next time you run the tests.

On the other hand it seems that Salesforce changes something with each login and expects to find the changed status the next time, in order to skip the second factor authentication. I am curious - did you have this problem?

I am using the Java Selenium bindings with Firefox. Maybe other browsers and language bindings do not have this problem - I was able to find an option to persist the changes in the browser profile in the C# language bindings, but not for java.

Here is a link to the related ticket in the Selenium issue tracker: https://code.google.com/p/selenium/issues/detail?id=1954

2

Please follow the Step:

  1. Login in your Org.
  2. Click on Setup then Administration Setup then Security Controls then Network Access now click on New .
  3. Set the IP address of your network or full IP range and save. (Eg. Start IP Address : 0.0.0.0 and End IP Address: 255.255.255.255 )
  4. Now use your script you should not get the verification screen.
1
  • you get Error: Invalid Data. if you have such broad range of ip
    – Nick
    Mar 17, 2018 at 16:09
0

Yes it is related to cookies, please create a custom profile and login to Salesforce, now use profile in your selenium code. It worked good for me.

0

This code handles 2FA (runs in normal mode, but could also run in headless mode). This creates a .selenium folder under the user's home folder, which is where the profile is created and cookies are saved.

So after running this script once and capturing the 2FA code, other scripts could be executed without being prompted for 2FA again.

import sys
from pathlib import Path
from urllib.parse import urlparse, urlunparse, urljoin
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By

# location of driver
DRIVER_PATH = '/usr/local/bin/chromedriver'

# selenium profile folder under home folder (saves cookies, etc)
SELENIUM_FOLDER = str(Path.joinpath(Path.home(), ".selenium"))

options = Options()

# set selenium profile folder
options.add_argument(f"user-data-dir={SELENIUM_FOLDER}")
options.add_argument("--window-size=1280,800")

service = ChromeService(executable_path=DRIVER_PATH)
driver = webdriver.Chrome(options=options, service=service)

try:
    driver.get("https://test.salesforce.com")

    # prompt user for their login credentials to sandbox
    user = input("Enter your Salesforce username: ")
    pw = input("Enter your Salesforce password: ")

    # find and populate login form, log user in
    username = driver.find_element(By.ID, "username")
    password = driver.find_element(By.ID, "password")
    login = driver.find_element(By.ID, "Login")
    username.send_keys(user)
    password.send_keys(pw)
    login.click()

    # Handle 2FA if prompted in the browser
    try:
        smc = driver.find_element(By.ID, "smc")  # "smc" is the text input id
        verification_code = input("\nEnter the verification code sent via SMS: ")
        smc.send_keys(verification_code)
        driver.find_element(By.ID, "save").click()  # "save" is the submit button id
    except NoSuchElementException:
        # not prompted for 2FA, so do nothing
        pass

    # Wait for various redirects before hitting the final home page
    try:
        WebDriverWait(driver, 20).until(ec.url_contains("/lightning/page/home"))
    except TimeoutException as e:
        driver.quit()
        sys.exit(f"\nERROR: unexpected page reached after login: {driver.current_url}\n")

    # logged in, continue with scripted interaction

# Very important!  This finally block is needed to quit the driver regardless
# of success or error.  If you don't quit the driver, the next selenium run
# will have an error, and you'll have to delete the contents of the .selenium
# folder.  Which will result in being prompted again for 2FA...
finally:
    print("\nClosing selenium...\n")
    driver.quit()

-1

Go to Setup->Manage Users->Your user Profile->Check your profile type(for example. mine was System Administrator)->Click on it->go to 'Login IP ranges'->Give IP Start Address as 0.0.0.0 and IP End Address as 255.255.255.255 and Save it. No authentication code would be generated for login in any browser/any system if your hitting IP address resides between (0.0.0.0 to 255.255.255.255).

No need to modify your automation code.

1
  • This essentially disables 2FA, which is a big security concern.
    – Ryan Guest
    Feb 18, 2016 at 7:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .