python 下拉菜单中的硒选择选项

python selenium selection option from dropdown

我需要帮助 select 使用 python 和 selenium 从以下下拉列表中选择“过去 7 天”选项:

这是下拉列表的一部分 html:

<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>

到目前为止我的代码是:

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()

我收到错误:

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

这只是我的第二个 python 项目,所以请原谅任何缺乏理解的地方,这只是一个爱好,但我已经坚持了 2 天,尝试了各种方法,但没有任何效果,任何帮助将不胜感激(甚至链接到有助于解决这个问题的视频,以便我学习),谢谢

我平时做的是:

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

其中 nameOfYourButtonTextToBeSelected 必须替换为您的特定变量

特别是,我可以看到 TextToBeSelected 应该是 Last 7 days,而从您显示的 HTML 片段中我看不到按钮的名称。

您可以像这样在 xpath 中对文本进行硬编码:

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()

如果您需要其他选项,请更改文本。

使用 WebDriverWait() 并跟随 xpath 点击 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()