简体   繁体   中英

How do I click on a drop down menu that is hidden using Selenium?

I'm trying to click on a drop down menu, but since it is hidden, I'm getting the error:

could not be scrolled into view

I've done some digging and I see that using some JavaScript could help, but I'm not sure how to implement that into my Python script.

<div class="MuiSelect-root MuiSelect-select MuiSelect-selectMenu MuiSelect-outlined MuiInputBase-input MuiOutlinedInput-input jss987" tabindex="0" role="button" aria-haspopup="listbox" aria-labelledby="input-label-idTeam1Desktop select-idTeam1Desktop" id="select-idTeam1Desktop"><span>​</span></div>
<input name="idTeam1Desktop" type="hidden" id="idTeam1Desktop" value="">

This is what I have so far:

driver = webdriver.Firefox(profile, options=options)
driver.get("https://tradenba.com/trade-machine")
element = driver.find_element_by_xpath("//*[@id='idTeam1Desktop']")
element.click()

To click on the drop down menu and select the menu item with text as MIL you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following based Locator Strategies :

  • Using XPATH :

     driver.get('https://tradenba.com/trade-machine') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='select-idTeam1Desktop']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='MuiListItemText-root MuiListItemText-inset']/span/div/p[text()='MIL']"))).click()
  • 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
  • Browser Snapshot:

贸易场

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