【问题标题】:Selenium Webdriver Python 2.x - select value from the listSelenium Webdriver Python 2.x - 从列表中选择值
【发布时间】:2017-03-05 01:39:44
【问题描述】:

我在 Selenium Webdriver 和 Python 2.x 中从列表中选择值时遇到问题。查看我的测试站点的 HTML 代码,下面是我遇到问题的 Selenium Webdriver 代码。

来自我的测试站点 (allegro.pl) 的 HTML

 <div class="input-text-wrapper">
                                <input id="main-search-text" type="text" name="string" x-webkit-speech autocomplete="off" required="required"
                                     class="autofocus" 
                                       value=""
                                       placeholder="czego szukasz?"/>
                            </div>
                    
                            <select class="search-type-select" name="search_scope">
                                    <option value="">wszystkie działy</option>
                                    <option value="electronics">Elektronika</option>
                                    <option value="fashion.beauty">Moda i uroda</option>
                                    <option value="household.health">Dom i zdrowie</option>
                                    <option value="baby">Dziecko</option>
                                    <option value="culture.entertainment">Kultura i rozrywka</option>
                                    <option value="sports.leisure">Sport i wypoczynek</option>
                                    <option value="automotive">Motoryzacja</option>
                                    <option value="collections.art">Kolekcje i sztuka</option>
                                    <option value="company.services">Firma</option>
                                    <option value="user">Użytkownicy</option>
                                    <option value="closed">Zakończone</option>
                                <option class="custom-search-type"></option>
                            </select>
                    
                            <input type="submit" value="Szukaj" class="sprite search-btn"/>
                    
                            <div class="search-type-select"
                    >
                                <ul>
                                        <li class=""><a class="active" href="#">wszystkie działy</a></li>
                                        <li class="department"><a  href="#electronics">Elektronika</a></li>
                                        <li class="department"><a  href="#fashion.beauty">Moda i uroda</a></li>
                                        <li class="department"><a  href="#household.health">Dom i zdrowie</a></li>
                                        <li class="department"><a  href="#baby">Dziecko</a></li>
                                        <li class="department"><a  href="#culture.entertainment">Kultura i rozrywka</a></li>
                                        <li class="department"><a  href="#sports.leisure">Sport i wypoczynek</a></li>
                                        <li class="department"><a  href="#automotive">Motoryzacja</a></li>
                                        <li class="department"><a  href="#collections.art">Kolekcje i sztuka</a></li>
                                        <li class="department"><a  href="#company.services">Firma</a></li>
                                        <li class="separator-top"><a  href="#user">Użytkownicy</a></li>
                                        <li class=""><a  href="#closed">Zakończone</a></li>
                                </ul>
                                <strong data-filter-id="" title="wszystkie działy">
                                    <span>wszystkie działy</span>
                                    <i class="sprite"></i>
                                </strong>
                            </div>

我的 Selenium Webdriver 代码:

driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://www.allegro.pl")
searchbox = driver.find_element_by_id("main-search-text")
searchbox.send_keys("what you search")

#And here is my problem, i wanna choose something from the list:
searchlist = find_element_by_css_selector("select#search_scope/option[text()='Elektronika']").click()

#I try another think here, but also not working:
#Select(driver.find_element_by_css_selector("select#search_scope")).select_by_value(baby).click()

我在位置栏“search_scope”中看到,但未选择值。

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver webdriver


    【解决方案1】:

    您要使用的语法webelement#webelement_name 仅适用于id 属性,但不适用于namewebelement#webelement_id

    所以这个"select#search_scope" 意味着搜索&lt;select id="search_scope"&gt;,在你的情况下,考虑到提供的HTML 样本,应该返回False

    尝试使用Select(),如下:

    from selenium.webdriver.support.ui import Select
    
    select = Select(driver.find_element_by_css_selector("select[name='search_scope']"))
    select.select_by_visible_text('Elektronika')
    

    如果这种方法不起作用,请尝试:

    driver.find_element_by_xpath('//span[text()="wszystkie dzialy"]').click()
    driver.find_element_by_link_text('Elektronika').click()
    

    【讨论】:

    • 不工作,和以前一样的反应。在 url 我可以看到“search_scope =”但没有价值。我的代码:
    • 这是什么reaction?可以分享异常日志吗?
    • @PythonLearn,检查更新的答案并告诉我结果
    • #Loginbutton submit driver.find_element_by_xpath('//span[text()="wszystkie dzialy"]').click() driver.find_element_by_link_text('Elektronika').click() searchbox = driver .find_element_by_id("main-search-text") searchbox.send_keys("telewizor") 只搜索“telewizor”而不从列表中选择任何位置。不点击列表。进程以退出代码 0 结束
    • 我想我发现我的错了。看看我在 HTML 代码中的第一篇文章,我认为 webelement 很糟糕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2021-01-08
    • 2018-06-14
    相关资源
    最近更新 更多