[selenium] Dropdown box selection

Selenium is about finding the elements to do action upon, the dropdown box exercise can be found here. From the web developer tool of your selected browser find the element of option1 and option2 and copy their xpaths, xpath is the most accurate for selenium to act upon, my preference is always xpath then next is css_selector if xpath for some reason does not work.

This is the original state.
This is the state after selenium clicks on option 1 element.
After a random pause, selenium clicks on option 2 element.

Below is the code for this exercise.

from selenium import webdriver
import os
from time import sleep
from random import uniform

def find_file(base="C:\\", name=None):
    Find file and return the first match found.
    :param base: Base path to search
    :param name: filename to search
    :return: absolute path of the file.
    for root, dirs, filename in os.walk(base):
        if name in filename:
            return os.path.join(root, name)

driver = webdriver.Firefox(executable_path=find_file(name="geckodriver.exe"))

# Select option1.

# use sleep to temporarily pause before next click.
sleep(uniform(5, 10))

# select option2.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s