Home > Blockchain >  How to handle dropdown without select in selenium python
How to handle dropdown without select in selenium python

Time:04-12

Hello I would like to be able to change the value of "50 Profiles / Page" to "500 Profiles / Page", but the problem is that in the HTML there is no "Select" tag.

I tried doing this but it didn't work

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

Here is the code The HTML code

<li >
    <div >
        <span  unselectable="on" aria-hidden="true">
            <span ></span></span>
                <div >
                        <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  style="position: relative;">
                            <div >
                                    <div 
                                        style="display: flex; flex-direction: column;">
                                        <div aria-selected="false" 
                                            title="20 Profiles / Page">
                                            <div >20 Profiles / Page</div><span
                                                 unselectable="on" aria-hidden="true"
                                                style="user-select: none;"><span
                                                    ></span></span>
                                        </div>
                                        <div aria-selected="false" 
                                            title="500 Profiles / Page">
                                            <div >500 Profiles / Page</div><span
                                                 unselectable="on" aria-hidden="true"
                                                style="user-select: none;"><span
                                                    ></span></span>
                                        </div>
                ...
</li>

CodePudding user response:

First we need to close the pop-ups and then try to click on pagination options. And using both Implicit wait and Explicit wait is not Recommended.

Try the following solution:

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

CodePudding user response:

First xpath to click dropdown:

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

Second xpath to click the option for 500 pages:

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

Here is a cheatsheet for relative xpaths https://devhints.io/xpath

Please be aware that browsers use xpath 1.0 and selenium also only supports 1.0, So some things like 'ends-with' won't work.

  • Related