【问题标题】:How to Click an Element inside a p-dropdown tag using Selenium and Python如何使用 Selenium 和 Python 单击 p-dropdown 标签内的元素
【发布时间】:2018-08-01 17:47:14
【问题描述】:
<p-dropdown _ngcontent-c16="" autofocus="" placeholder="Select Quota" class="ng-tns-c13-11 ui-inputwrapper-filled ng-untouched ng-pristine ng-valid">
  <div class="ng-tns-c13-11 ui-dropdown ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="width: 234px;">
    <div class="ui-helper-hidden-accessible ng-tns-c13-11 ng-star-inserted">
    <select class="ng-tns-c13-11" aria-hidden="true" tabindex="-1" aria-label="A">
      <option class="ng-tns-c13-11 ng-star-inserted">Select</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="GN">A</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="SS">B</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="LD">C</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="HP">D</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="TQ">E</option>
      <option class="ng-tns-c13-11 ng-star-inserted" value="PT">F</option>
      <!----></select>
    </div>

我需要在下拉列表中选择一个值 我尝试使用的 xpath 是:

driver.find_element_by_xpath("//*[@value='LD']").click()

但这表示未找到元素...还有什么其他表达式可用于在 DropDown 中选择选项?

也可以像下面提到的那样做

driver.find_element_by_xpath("//*[@placeholder='Select Quota']").click()

后面还有别的东西吗?

【问题讨论】:

  • 我之前使用send_keys 在下拉菜单中选择一个选项。
  • 发送键然后回车?

标签: python selenium select selenium-webdriver webdriverwait


【解决方案1】:

这个下拉菜单是使用 select 和 options 标签构建的。所以选择类应该工作。

你可以试试这段代码:

select = Select(driver.find_element_by_css_selector("select.ng-tns-c13-11"))

# select by visible text
select.select_by_visible_text('C')  

你必须做的进口:

from selenium.webdriver.support.ui import Select

【讨论】:

  • 如果您遇到任何问题,请使用 import 更新答案。
  • 表示元素当前不可见,无法操作
  • 然后在 Select() 中使用 wait.until(EC.visibilty_of_element_located((By.CSS_SELECTOR(“select.ng-tns-c13-11”)))
  • 不! select = Select(driver.find_element_by_css_selector("select.ng-tns-c13-11"))-->工作正常但是,select.selct_by_visible_text('C') 是发生异常的地方
  • 你可以等待一段时间再执行select.select_by_visible_text('C')
【解决方案2】:

由于&lt;select&gt; 元素是Angular 元素,因此您必须诱导WebDriverWait 以使&lt;select&gt; 元素可点击 并利用Select类你可以使用以下解决方案:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
# other lines of code
mySelect = Select(WebDriverWait(driver, 20).until(EC.visibilty_of_element_located((By.XPATH, "//select[contains(@class,'ng-tns-') and @aria-label='A']"))))
# select by value
mySelect.select_by_value("LD")

【讨论】:

  • 试试更新的答案,让我知道状态
【解决方案3】:

只需要先等待LD元素可点击,像这样……

from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@value='LD']")))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2022-11-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多