【问题标题】:How to select this element from this non select drop-down menu using Selenium Webdriver and Python如何使用 Selenium Webdriver 和 Python 从这个非选择下拉菜单中选择这个元素
【发布时间】:2021-12-30 00:05:22
【问题描述】:

我正在尝试使用 Selenium 从该页面的下拉菜单中选择第二个选项(带有文本“24 个单词”):https://www.myetherwallet.com/wallet/access/software?type=mnemonic

我可以使用这段代码点击下拉菜单来显示 2 个选项:

drop_down_menu = driver.find_element(By.XPATH, '//div[@class="v-select__slot"]')
drop_down_menu.click()

但是我无法选择第二个选项(或任何选项)。我无法使用 Selenium Select 类,因为下拉菜单元素不是 Select 类型元素。我尝试了以下 2 段代码但没有成功:

select_24_words = WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(
            (By.CSS_SELECTOR, 'div#list-item-252-1.v-list-item.v-list-item--link.theme--light'),'24'))
select_24_words.click()

select_24_words = driver.find_element(By.XPATH, '//div[@id="list-item-252-1"]')
select_24_words.click()

谁能帮忙?

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    使用element_to_be_clickable 条件作为xpath - //div[text()='24 words'] 并且能够点击24 word 选项。

    尝试如下一次。

    driver.get("https://www.myetherwallet.com/wallet/access/software?type=mnemonic")
    
    wait = WebDriverWait(driver,30)
    
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"v-select__selections"))).click()
    
    wait.until(EC.element_to_be_clickable((By.XPATH,"//div[text()='24 words']"))).click()
    

    【讨论】:

      【解决方案2】:

      下拉菜单不是传统的 元素,因此您不能使用Select 类。

      要从下拉菜单中选择第二个选项(文本为“24 个单词”),您需要为element_to_be_clickable() 引入WebDriverWait,您可以使用以下任一Locator Strategies

      • 使用XPATH:

        driver.get("https://www.myetherwallet.com/wallet/access/software?type=mnemonic")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='v-select__selections']"))).click()
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='v-list-item__title' and text()='24 words']"))).click()
        
      • 浏览器快照:

      • 注意:您必须添加以下导入:

        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        

      【讨论】:

        【解决方案3】:

        您需要点击下拉栏。然后是默认选项。

        然后你可以点击第二个选项。

        您只需要一个显式等待。

        driver = webdriver.Chrome(driver_path)
        driver.maximize_window()
        driver.get("https://www.myetherwallet.com/wallet/access/software?type=mnemonic")
        default_option = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.v-select__selection--comma")))
        default_option.click()
        
        second_option = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='24 words']/ancestor::div[contains(@id,'item')]")))
        second_option.click()
        

        进口:

        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-17
          • 1970-01-01
          • 2017-10-29
          • 1970-01-01
          相关资源
          最近更新 更多