question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to reproduce user behavior with selenium on Python

See original GitHub issue

🐛 Bug Report

Hi.

I am trying to register a user in this practice site: http://practice.automationtesting.in/my-account/

The process is very simple, write a user-email, write a password and then click on register button.

As user, you manually do the steps before describe and you could register a user. but if you do with selenium, you can’t do it.

I discovered that manually you have a message about strong or weak password, and this message doesn’t appear with selenium. But there are more. If I use record and play process with selenium IDE, I can reproduce the user behave, as I show in the video.

https://user-images.githubusercontent.com/37479902/120584864-1c16bf00-c407-11eb-914a-48fa00e22817.mov

I tried with Chrome and Firefox, but the result was the same.

So, I share technical information.

OS: Windows 10 x64
Python: 3.9.3
Pytest: 6.2.4
Selenium: 3.141.0
Chrome:  91.0.4472.77
Firefox: 89.0
Selenium IDE: 3.17.0

I also share the Python script:

import pytest
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec


driver = None
def setup():
    global driver
    chrome_driver_path = #path to chrome driver
    gecko_driver_path =  #path to gecko driver
    url = 'http://practice.automationtesting.in/my-account/'

    browser = 'chrome'
    if browser == 'chrome':
        driver = webdriver.Chrome(executable_path=chrome_driver_path)
    elif browser == 'firefox':
        driver = webdriver.Firefox(executable_path=gecko_driver_path)
    else:
        raise ValueError('Invalid Browser')

    driver.maximize_window()
    driver.set_page_load_timeout(10)
    driver.implicitly_wait(5)
    try:
        driver.get(url)
    except TimeoutException:
        print('Page not load')
        driver.quit()

def test_register_user():
    test_data = {
        'username' : 'user_mail_test@gmail.com',
        'password' : 'StR0nGp4AsSw0Rd'
    }
    driver.find_element_by_id('reg_email').send_keys(test_data['username'])
    driver.find_element_by_id('reg_password').send_keys(test_data['password'])
    wait_driver = WebDriverWait(driver,5,0.2)
    try:
        button = wait_driver.until(ec.element_to_be_clickable((By.NAME,'register')))
        button.click()
    except TimeoutException:
        pytest.fail('Register button is not clickable')


def teardown():
    driver.quit()

To execute the script I use:

python -m pytest path\to\script.py

What could be happening?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
diemolcommented, Jun 3, 2021

💬 It looks like this is a question rather than an issue. 💡 A better way to address this is:

0reactions
diemolcommented, Jun 7, 2021

The issue lays on the website under test, if you check the page source, it is full of JS scripts at the end that need quite a while to load. Specifically, for password checks, this script is used. If the JS code has not loaded and you try to interact with the input fields, you won’t see the behaviour you are expecting.

JS code will take a while to load because every test executed with WebDriver it is using a fresh profile, where nothing has been cached. The IDE is using the current browser profile and session, which is why all that JS loads faster (cached).

I tried your script and I don’t think it has any issue, it is just that the page you want to test is taking too long to load. To evidentiate that, I added a “only for demo purposes” sleep, and then you can see how the code works well. If you want to keep learning on this website, I would suggest to contact the owners to improve the site’s performance and make it more testable, or to find a specific condition you can wait for before interacting with the website.

In the end, I believe this is not an issue with Selenium, rather with the website under test.

import pytest
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec


driver = None
def setup():
    global driver
    chrome_driver_path = '/usr/local/bin/chromedriver'
    gecko_driver_path = '/usr/local/bin/geckodriver'
    url = 'http://practice.automationtesting.in/my-account/'

    browser = 'chrome'
    if browser == 'chrome':
        driver = webdriver.Chrome(executable_path=chrome_driver_path)
    elif browser == 'firefox':
        driver = webdriver.Firefox(executable_path=gecko_driver_path)
    else:
        raise ValueError('Invalid Browser')

    driver.maximize_window()
    driver.set_page_load_timeout(10)
    # Do not mix implicit and explicit waits
    # driver.implicitly_wait(5)
    try:
        driver.get(url)
    except TimeoutException:
        print('Page not load')
        driver.quit()


def test_register_user():
    test_data = {
        'username' : 'user_mail_test@gmail.com',
        'password' : 'StR0nGp4AsSw0Rd'
    }
    # Page is full of css and js, find what you want to wait for to be present
    # before inreacting with the script, so validations are shown as expected.
    # As a workaround to show the issue is in the website, not in Selenium, and old fashioned
    # sleep to let all that JS load before interacting with the page.
    time.sleep(20)
    driver.find_element_by_id('reg_email').send_keys(test_data['username'])
    driver.find_element_by_id('reg_password').send_keys(test_data['password'])
    wait_driver = WebDriverWait(driver, 5, 0.2)
    try:
        button = wait_driver.until(ec.element_to_be_clickable((By.NAME,'register')))
        button.click()
    except TimeoutException:
        pytest.fail('Register button is not clickable')


def teardown():
    driver.quit()

If you have any more questions, please join us at our IRC/Slack channel.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Selenium webdriver with Python can't reach to a website
The error points out that it was unable to find the XPATH element, which is why it errored out. The main causes for...
Read more >
How to download file using Selenium & Python | BrowserStack
Step-by-step tutorial on how to download a file from a website using Selenium and Python. Also, learn how to download files to a...
Read more >
7. WebDriver API — Selenium Python Bindings 2 documentation
Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with....
Read more >
Selenium Python Tutorial: Getting Started With ... - LambdaTest
In this Selenium Python tutorial, I'll show you how to use the BDD test framework called Behave with Selenium & Python particularly for ......
Read more >
Mimicking Human Activity using Selenium and Python
To replicate this project, you will need a web browser and correlating driver. We used Chrome and Chrome driver, Python, Numpy version 1.21.1, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found