Categories
Python Answers

How to wait until element is present, visible and interactable with Python Selenium?

Sometimes, we want to wait until element is present, visible and interactable with Python Selenium.

In this article, we’ll look at how to wait until element is present, visible and interactable with Python Selenium.

How to wait until element is present, visible and interactable with Python Selenium?

To wait until element is present, visible and interactable with Python Selenium, we can use the wait.until method.

For instance, we write

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#...

browser.find_element_by_css_selector(".reply-button").click()

wait = WebDriverWait(browser, 10)
email = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.anonemail'))).get_attribute("value")
print(email)

to create the wait object with

wait = WebDriverWait(browser, 10)

Then we call wait.until with EC.visibility_of_element_located((By.CSS_SELECTOR, '.anonemail')) to wait for the element with the anonemail class to be visible.

We set the timeout to 10 seconds.

Conclusion

To wait until element is present, visible and interactable with Python Selenium, we can use the wait.until method.

Categories
Python Answers

How to fix OperationalError No Such Table with Python Django?

Sometimes, we want to fix OperationalError No Such Table with Python Django.

In this article, we’ll look at how to fix OperationalError No Such Table with Python Django.

How to fix OperationalError No Such Table with Python Django?

To fix OperationalError No Such Table with Python Django, we run the migrations to add the missing table.

We run

python manage.py migrate --run-syncdb 

to create the table without migrations with --run-syncdb.

We can also make and run migrations with

python manage.py makemigrations app
python manage.py migrate app

Conclusion

To fix OperationalError No Such Table with Python Django, we run the migrations to add the missing table.

Categories
Python Answers

How to show the verbose version of a choice with Python Django templates?

Sometimes, we want to show the verbose version of a choice with Python Django templates.

In this article, we’ll look at how to show the verbose version of a choice with Python Django templates.

How to show the verbose version of a choice with Python Django templates?

To show the verbose version of a choice with Python Django templates, we can create a model class with our own method to return the verbose version of the choice.

For instance, we write

from django.db import models

class Scoop(models.Model):
    FLAVOR_CHOICES = [
        ('c', 'Chocolate'),
        ('v', 'Vanilla'),
    ]

    flavor = models.CharField(choices=FLAVOR_CHOICES)

    def flavor_verbose(self):
        return dict(Scoop.FLAVOR_CHOCIES)[self.flavor]

to create the flavor_verbose method.

In it, we convert FLAVOR_CHOICES to a dict with dict(Scoop.FLAVOR_CHOCIES).

And then we get the verbose version of the choice with self.flavor.

Then in our template, we write

{{ scoop.flavor_verbose }}

to show the choice.

Conclusion

To show the verbose version of a choice with Python Django templates, we can create a model class with our own method to return the verbose version of the choice.

Categories
Python Answers

How to add a ChoiceField with Django Rest Framework?

Sometimes, we want to add a ChoiceField with Django Rest Framework.

In this article, we’ll look at how to add a ChoiceField with Django Rest Framework.

How to add a ChoiceField with Django Rest Framework?

To add a ChoiceField with Django Rest Framework, we can set the source argument of the field.

For instance, we write

class User(AbstractUser):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )

    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)

to create a CharField in the User model.

Then we write

class UserSerializer(serializers.ModelSerializer):
    gender = serializers.CharField(source='get_gender_display')

    class Meta:
        model = User

to create the gender field in the UserSerializer to set the source argument to the 'get_gender_display' method.

get_gender_display is a method that’s automatically included with the User model.

Conclusion

To add a ChoiceField with Django Rest Framework, we can set the source argument of the field.

Categories
Python Answers

How to add Python Django unit tests without a database?

Sometimes, we want to add Python Django unit tests without a database.

In this article, we’ll look at how to add Python Django unit tests without a database.

How to add Python Django unit tests without a database?

To add Python Django unit tests without a database, we can create our own test runner class.

For instance, we write

from django.test.runner import DiscoverRunner

class NoDbTestRunner(DiscoverRunner):
  def setup_databases(self, **kwargs):
    pass

  def teardown_databases(self, old_config, **kwargs):
    pass

to add the NoDbTestRunner that overrides the code to set up the test database in setup_databases.

And in teardown_databases we override the code to clean the database after tests are run.

Then we create a settings file that makes Django use the test runner that we just created with

from mysite.settings import *

TEST_RUNNER = 'mysite.scripts.testrunner.NoDbTestRunner'

We put that in the no_db_settings.py file.

Then when we run our test, we run

python manage.py test myapp --settings='no_db_settings'

to run our tests with the no_db_settings.py file as the settings file.

Conclusion

To add Python Django unit tests without a database, we can create our own test runner class.