Shantanu Bedajna
Shantanu Bedajna

Reputation: 581

Python How to click on auto complete in selenium

I am trying to click on a auto complete search bar results on the website https://homely.com.au so i can search for the resulats

i want to click on the drop down element that appears after the pasting of the city name so i can search with the full city name and get the results

here is the search bar code of the website

<div class="SearchAutoComplete SearchAutoComplete--isDropdown SearchAutoComplete--hasIcon">
    <div class="SearchAutoComplete-searchIcon">
        <span role="presentation" class="icon-wrapper">
            <svg class="icon icon-search">
                <use
                    xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-search">
                </use>
            </svg>
        </span>
    </div>
    <input type="text" class="SearchAutoComplete-input" placeholder="Search by agent name or suburb" value="Aberdare">
        <div class="SearchAutoComplete-clear animated fadeIn">
            <span role="presentation" class="icon-wrapper">
                <svg class="icon icon-close">
                    <use
                        xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-close">
                    </use>
                </svg>
            </span>
        </div>
        <ul class="SearchAutoComplete-results hide">
            <li class="heading">
                <span role="presentation" class="icon-wrapper">
                    <svg class="icon icon-map">
                        <use
                            xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-map">
                        </use>
                    </svg>
                </span>
                <span class="heading-text">locations</span>
            </li>
            <li data-field-index="0" data-index="0" class="SearchAutoComplete-item active">
                <a>Yabbra, New South Wales</a>
            </li>
        </ul>
    </div>

here is the inspect element of the search bar and the results if that helps

enter image description here

AND THE RESULTS INSPECT ELEMENT

enter image description here

and here is my selenium code

    driver.get('https://homely.com.au/')
    wait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Agent Finder')]"))).click()
    try:
        wait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'No thanks')]"))).click()
    except:
        pass
#THE PROBLEM STARTS FROM HERE
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).clear()
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).send_keys(tiki)
    time.sleep(1)
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).click()
    time.sleep(1)
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).click()
    time.sleep(1)
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).send_keys(Keys.DOWN)
    time.sleep(1)
    wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.SearchAutoComplete-input'))).send_keys(Keys.ENTER)
    time.sleep(1)
    try:
        wait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Start comparing agents')]"))).click()
    except:
        pass

i am trying to input a city name and click on it (cause otherwise search won't show) and then click on the field once again press down arrow and then press enter

here is the page enter image description here

how i click on the auto complete search bar

and then this page comes with result

enter image description here

but the problem is my solution is inconsistent and search sometimes works sometimes doesn't any better robust way to do this with selenium ?

Upvotes: 1

Views: 3599

Answers (1)

Phung Duy Phong
Phung Duy Phong

Reputation: 896

Could you try this:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('https://www.homely.com.au/agents')

search = driver.find_element_by_xpath(
                    "//input[starts-with(@class, 'SearchAutoComplete-input')]")
search.clear()
search.send_keys('ge')
auto_complete = driver.find_elements_by_xpath(
        "//li[starts-with(@class, 'SearchAutoComplete-item')]")
auto_complete[0].click()

Just comment so I can fix the code more to suit your needs

Upvotes: 2

Related Questions