【问题标题】:Using Selenium to get past dropdown (md-select)使用 Selenium 跳过下拉菜单(md-select)
【发布时间】:2019-01-06 03:32:08
【问题描述】:

我遇到了在 Selenium 中无法通过的下拉菜单。 我正在尝试使用 Selenium 从此链接收集一些价格数据: https://xxx。在此链接中,您需要点击一个按钮(下一步),然后在随后的下拉列表中选择任意选项,然后再次按(下一步)进入我想收集一些信息的信息页面。我卡在下拉列表中 - 我无法选择任何选项。

这是我目前的代码:

browser.get("https://xxx/#/pricePlans/step1")
wait = WebDriverWait(browser, 10)
while True:
    try:        
        button = browser.find_element_by_css_selector('body > div.md-dialog-container.ng-scope > md-dialog > md-dialog-actions > div > button')
    except TimeoutException:
        break    
    button.click()
    options_box= browser.find_element_by_class_name('bullet-content-title')
    wait = WebDriverWait(browser, 5)
    options_box.click() 

问题在于下拉选项(它有 HDB 1-room、HDB 2-room 等选项)。我试图通过 XPATH、CSS 选择器、class_name(如上所示)引用选项框,但使用上面的 sn-p,Spyder 会超时。我尝试过的其他 sn-ps 包括:

ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "bullet-content-title")))

使用 XPATH、class_name 但没有运气。 我是网络抓取的新手,到目前为止通过搜索 SO,但我无法找到有关 (md-select) 下拉列表的很多解决方案。 我也尝试使用

ActionChains(driver).move_to_element(options_box).click(options_box)

但我没有看到任何点击或鼠标移动,所以我很难过。 我很感激此时的任何建议。太感谢了!

编辑: 代码片段和响应:

from selenium import webdriver 
from selenium.webdriver.support import ui
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
option = webdriver.ChromeOptions()
option.add_argument('--incognito')
browser = webdriver.Chrome(executable_path='C:\\ChromeDriver\\chromedriver.exe', options=option)
browser.get("https://xxx")

wait = WebDriverWait(browser, 10)

while True:
    try:        
        button = browser.find_element_by_css_selector('body > div.md-dialog-container.ng-scope > md-dialog > md-dialog-actions > div > button')
    except TimeoutException:
        break    
    button.click()


    options_box = browser.find_element_by_class_name('bullet-content-title')
    wait = WebDriverWait(browser, 5)
    options_box.click()

这将返回“StaleElementReferenceException:过时的元素引用:元素未附加到页面文档” 我认为这是由于第二个“下一步”按钮的存在,该按钮目前是惰性的。

options_box = ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "bullet-content-title")))  
options_box.click()

什么都不做。 Spyder 最终返回给我 TimeOut 错误。

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    @AndrewRay 的答案有利于获得价值,但不适用于选择选项。你可以这样做来选择选项。

    #browser.get("https://......")
    wait = WebDriverWait(browser, 10)
    
    try:        
        browser.find_element_by_css_selector('button.green-btn').click()
        # wait until dialog dissapear
        wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, 'md-dialog[aria-describedby="dialogContent_0"]')))
        # click the dropdown
        browser.find_element_by_css_selector('md-input-container').click()
        # select the option element
        setOptionElement = browser.find_element_by_css_selector('md-option[value="HDB Executive"]')
        # need to scrollIntoView if the option in the bottom
        # or you get error the element not clickable
        browser.execute_script('arguments[0].scrollIntoView();arguments[0].click()', setOptionElement)
    
    except Exception as ex:
        print(ex)
    

    【讨论】:

    • 谢谢。是的。这适用于选择一个选项。也许我刚才在我的兴奋中对我的cmets不太清楚。谢谢@ewwink!
    【解决方案2】:
    driver.get('https://compare.openelectricitymarket.sg/#/pricePlans/step1')
    time.sleep(5)  
    
    next_btn = driver.find_element_by_css_selector('button.green-btn')  
    next_btn.click()  
    
    dropdown = driver.find_element_by_id('select_4')  
    options = dropdown.find_elements_by_tag_name('md-option')  
    
    for option in options:  
       print option.get_attribute('value')
    

    希望这会有所帮助。使用 .get_attribute 方法查找选项的值,如果匹配所需的值,请单击该选项。 :)

    【讨论】:

    • 添加了一个新的答案,因为您无法在 SO 的评论部分格式化代码,所以就这样吧! :)
    • 嗨,安德鲁,看起来不错!嗯,看看你的代码,看来我一直在寻找错误的元素?
    • 您似乎找到了该元素,但尝试单击下拉菜单本身,但下拉菜单的选项位于 html 中,但在单击之前隐藏,因此您只需单击该选项在后端,它将出现在下拉列表中。无需单击下拉菜单本身。
    • 嗨 Andrew,您知道如何选择下拉菜单中的任何一个选项吗?
    • 当我使用以下插件时:for option in options: if option.get_attribute('value')=='HDB 4-Room': option.click() 我得到一个“ElementNotVisibleException: element不可交互”
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 2020-04-28
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多