如何在 selenium python 中处理没有 select 的下拉菜单

How to handle dropdown without select in selenium python

您好,我希望能够将“50 个个人资料/页面”的值更改为“500 个个人资料/页面”,但问题是 HTML 中没有“Select”标签。

我试过这样做但没用

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

url = 'https://www.personality-database.com/profile?pid=1&sort=hot'

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(url)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/section/main/div[1]/div[2]/div/div[5]/ul/li[10]/div/div[1]/span[2][text()="500 Profiles / Page"]'))).click()

这是代码HTML代码

<li class="rc-pagination-options">
    <div class="rc-select rc-pagination-options-size-changer rc-select-single rc-select-show-arrow">
        <span class="rc-select-arrow" unselectable="on" aria-hidden="true">
            <span class="rc-select-arrow-icon"></span></span>
                <div class="rc-select-dropdown rc-select-dropdown-placement-topLeft  rc-select-dropdown-hidden">
                        <div role="listbox" id="rc_select_0_list">
                            <div aria-label="20 Profiles / Page" role="option" id="rc_select_0_list_0"
                                aria-selected="false">20</div>
                        </div>
                        <div class="rc-virtual-list" style="position: relative;">
                            <div class="rc-virtual-list-holder">
                                    <div class="rc-virtual-list-holder-inner"
                                        style="display: flex; flex-direction: column;">
                                        <div aria-selected="false" class="rc-select-item rc-select-item-option"
                                            title="20 Profiles / Page">
                                            <div class="rc-select-item-option-content">20 Profiles / Page</div><span
                                                class="rc-select-item-option-state" unselectable="on" aria-hidden="true"
                                                style="user-select: none;"><span
                                                    class="rc-select-item-option-state-icon"></span></span>
                                        </div>
                                        <div aria-selected="false" class="rc-select-item rc-select-item-option"
                                            title="500 Profiles / Page">
                                            <div class="rc-select-item-option-content">500 Profiles / Page</div><span
                                                class="rc-select-item-option-state" unselectable="on" aria-hidden="true"
                                                style="user-select: none;"><span
                                                    class="rc-select-item-option-state-icon"></span></span>
                                        </div>
                ...
</li>

单击下拉列表的第一个 xpath:

//div[@class='rc-select rc-pagination-options-size-changer rc-select-single rc-select-show-arrow']

点击 500 页选项的第二个 xpath:

//div[@class='rc-select-item-option-content']/self::div[text()='500 Profiles / Page']

这是相对 xpath 的备忘单 https://devhints.io/xpath

请注意,浏览器使用 xpath 1.0,selenium 也只支持 1.0, 所以像 'ends-with' 这样的东西是行不通的。

首先我们需要 close the pop-ups 然后尝试 click on pagination options。 并且不推荐同时使用隐式等待和显式等待。

尝试以下解决方案:

driver.get("https://www.personality-database.com/profile?pid=1&sort=hot")
wait = WebDriverWait(driver,30)
try:
    # Close the footer add
    wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@id='ezmob-wrapper']/div/center/span/div/div/span"))).click()
    # Scroll a distance so that the Cookie pop up appears and Close it
    driver.execute_script("window.scrollBy(0,50);")
    wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@id='rcc-confirm-button']"))).click()
except:
    print("no adds")
 
# click on the drop down option   
pagination = wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='rc-pagination-options']")))
pagination.click()

# Click on the 500 profiles
option = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='rc-virtual-list-holder-inner']//div[text()='500 Profiles / Page']")))
option.click()