python selenium selection option from dropdown

Viewed 49

I need help to select the option "Last 7 days" from the following dropdown using python and selenium:

enter image description here

here is the html for part of the dropdown:

<div class="date-range" id="yui_3_18_1_1_1614002255126_175" style="margin-right: 25px;">
    <div class="option-select global default" id="yui_3_18_1_1_1614002255126_174">
        <div class="select-open" id="yui_3_18_1_1_1614002255126_173">
            <div class="select-title" id="yui_3_18_1_1_1614002255126_172">
                <span class="option-title">DATE RANGE:</span>
                <span class="option-selection" id="yui_3_18_1_1_1614002255126_170">Last 30 days</span>
            </div>
            <div class="dropdown-arrow"></div>
        </div>
        <div class="select-body" id="yui_3_18_1_1_1614002255126_190">
            <div class="option">
                Today
                <span class="extra-info-wrapper">
                    <span>&nbsp;(</span>
                    <span class="extra-info">22 Feb</span>
                    <span>)</span>
                </span></div>
            <div class="option">
                Yesterday
                <span class="extra-info-wrapper">
                    <span>&nbsp;(</span>
                    <span class="extra-info">21 Feb</span>
                    <span>)</span>
                </span></div>
            <div class="option">
                This week
                <span class="extra-info-wrapper">
                    <span>&nbsp;(</span>
                    <span class="extra-info">Monday - Today</span>
                    <span>)</span>
                </span></div>
            <div class="option" id="yui_3_18_1_1_1614002255126_189">
                Last 7 days
                <span class="extra-info-wrapper">
                    <span>&nbsp;(</span>
                    <span class="extra-info">16 Feb - Today</span>
                    <span>)</span>
                </span></div>
            <div class="option">
                This month
                <span class="extra-info-wrapper">
                    <span>&nbsp;(</span>
                    <span class="extra-info">1 Feb - Today</span>
                    <span>)</span>
                </span></div>

My code so far is:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
myUsername="Xxx”
myPassword="Xxx”
driver.get("https://uk.ixl.com/signin/sop")


driver.find_element_by_xpath('//*[@id="siusername"]').send_keys(myUsername)
driver.find_element_by_xpath('//*[@id="sipassword"]').send_keys(myPassword)
driver.find_element_by_xpath('//*[@id="custom-signin-button"]').click()
time.sleep(1)

#select report
driver.get("https://uk.ixl.com/analytics/students-quickview?teacherId=125756982")
time.sleep(5)           

wait = WebDriverWait(driver, 10)

wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'date-range'))).click()
driver.find_element_by_id('yui_3_18_1_1_1614002255126_189').click()

I am getting the error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"css selector","selector":"[id="yui_3_18_1_1_1614002255126_189"]"}

This is only my second python project so forgive any lack of understanding, this is just a hobby, but I've been stuck for 2 days trying all sorts but nothing works, any help would be appreciated (even links to videos that would help solve this so I can learn), thanks

3 Answers

You can hard code text in xpath like this:

wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'date-range'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//div[text()="Last 7 days"]'))).click()

Please change the text if you want other options.

What I usually do is:

driver.find_element_by_xpath(f'//select[@name="nameOfYourButton"]/option[text()=""TextToBeSelected"]').click()

Where nameOfYourButton and TextToBeSelected must be replaced with your specific variable

In particular, I can see that TextToBeSelected should be Last 7 days while from the HTML snippet that you are showing I can't see the name of the button.

Use WebDriverWait() and following xpath to click on Last 7 days.

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='select-body']//div[@class='option' and contains(.,'Last 7 days')]"))).click() 
Related