0
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0


browser = webdriver.Chrome('C:/Users/xyz/Downloads/chromedriver.exe')


# Define all variables required
urlErep = browser.get('http://www.erepublik.com')
xPathToSubmitButton = "//*[@id='login_form']/div[1]/p[3]/button"
urlAlerts = 'https://www.erepublik.com/en/main/messages-alerts/1'
one = 1
xPathToAlerts = "//*[@id='deleteAlertsForm']/table/tbody/tr[%d]/td[3]/p" %one


def logintoerep():
    email = browser.find_element_by_id("citizen_email")
    password = browser.find_element_by_id("citizen_password")

    email.send_keys('myemail')
    password.send_keys('mypassword')

    browser.find_element_by_xpath(xPathToSubmitButton).click()


logintoerep()

The text above is code I wrote using Selenium to login to erepublik.com.

My main goal is to verify some information on eRepublik.com whenever someone fills a Google Form, and then complete an action based on the Google Form data. I'm trying to login to eRepublik using Selenium, and in each attempt to run the script(which I need to run 24/7, so that whenever the form gets a new response the script is ran) it creates a new window, and after 10-20 times I've logged in to the website it asks for captcha which Selenium can't complete. While in my existing browser window, I'm already logged in so I don't have to worry about Captcha and can just run my code.

How can I bypass this problem? Because I need the script to be able to login every time on its own, but captcha won't allow that. The best solution would be to use Selenium on my existing browser windows, but it doesn't allow that.

Is is possible to copy some settings from my normal browser windows to the Selenium-run browser windows so that every time logs in automatically instead?

I'm open to any suggestions as long as they can get me to verify and complete a few minor actions in the website I've linked.

Deep
  • 3
  • 3

2 Answers2

0

You can attach your Chrome profile to Selenium tests

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
browser = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
Werewolfas
  • 46
  • 4
  • Ok, that is a great idea. However, when I tried to run that code I got the following error| selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64) | Any ideas on a fix? – Deep Aug 07 '18 at 07:27
  • Looks like this is a common problem [org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser](https://stackoverflow.com/questions/50642308/org-openqa-selenium-webdriverexception-unknown-error-devtoolsactiveport-file-d) – Werewolfas Aug 07 '18 at 07:30
  • None of them worked. I tried adding "disable-dev-shm-usage" and "sandbox" to options, didn't do anything. The new window does open up, but the error comes after that and it doesn't do anything else. – Deep Aug 07 '18 at 08:01
0

First off, CAPTCHAs are meant to do exactly that: repel robots/scripts from brute-forcing, or doing repeated actions on certain app features (e.g: login/register flows, send messages, purchase flows, etc.). So you can only go around... never through.

That being said, you can simulate the logged-in state by doing one of the following:

  • loading the authentication cookies required for the user to be logged in (usually it's only one cookie with a token of some sorts);
  • loading a custom profile in the browser that already has that user logged in;
  • use some form of basic auth when navigating to that specific URL (if the web-app has any logic to support this);

Recommended approach: Usually in most companies (at least from my exp), there usually is a specific cookie, or flag that you can set to disable CAPTCHAs for testing purposes. If this is not the case, talk to your PM/DEVs to create such a feature that permits the testing of your web-app.

Don't want to advertise advertise my content, but I think I best tackled this topic HERE. Maybe it can further help.

Hope you solve the problem. Cheers!

iamdanchiv
  • 3,828
  • 4
  • 32
  • 40