Python如何在selenium中单击自动完成

时间:2017-05-26 09:52:55

标签: javascript jquery python html selenium

我正在尝试点击网站https://homely.com.au上的自动完整搜索栏结果,以便我可以搜索结果

我想点击粘贴城市名称后显示的下拉元素,以便我可以搜索完整的城市名称并获得结果

这是网站的搜索条形码

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

这里是搜索栏的inspect元素,如果有帮助

,则为结果

enter image description here

和结果检查元素

enter image description here

,这是我的硒代码

    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

我正在尝试输入城市名称并点击它(因为否则搜索赢了&不显示)然后再次点击该字段按向下箭头然后按Enter键

这是页面 enter image description here

我如何点击自动完成搜索栏

然后此页面附带结果

enter image description here

但问题是我的解决方案不一致,有时搜索有时无效 用硒做任何更好的方法吗?

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

只需评论,以便我可以更多地修复代码以满足您的需求