简体   繁体   中英

How to select this element from this non select drop-down menu using Selenium Webdriver and Python

I'm trying to use Selenium to select the 2nd option (with the text "24 words") from the drop-down menu on this page: https://www.myetherwallet.com/wallet/access/software?type=mnemonic

I was able to click on the drop-down menu to display the 2 options using this piece of code:

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

However I was not able to select the 2nd option (or any options). I could not use Selenium Select class because the drop-down menu element is not of Select type element. I tried the below 2 pieces of code without any success:

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

and

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

Can anyone help?

Used element_to_be_clickable condition for the xpath - //div[text()='24 words'] and was able to click on 24 word option.

Try like below once.

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

The drop-down menu isn't a conventional element so you can't use Select class.

To select the 2nd option (with the text "24 words") from the drop-down menu you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using 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()
  • Browser Snapshot:

以太钱包

  • Note : You have to add the following imports:

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

You need to click on the drop-down bar. Then default option.

Then you can click on second option.

All you need is a Explicit Waits.

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

Imports:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM