简体   繁体   English

如何单击使用 Selenium 隐藏的下拉菜单?

[英]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.我已经进行了一些挖掘,发现使用一些 JavaScript 可能会有所帮助,但我不确定如何在我的 Python 脚本中实现它。

<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 :要单击下拉菜单和 select 菜单项,文本为MIL ,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下基于定位器策略

  • Using XPATH :使用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:浏览器快照:

贸易场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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