简体   繁体   English

如何使用Selenium和Python单击p-dropdown标记内的元素

[英]How to Click an Element inside a p-dropdown tag using Selenium and Python

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

I need to select a value inside the drop down the xpath I tried using is : 我需要在尝试使用的xpath的下拉列表中选择一个值:

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

But that says element not found...What other expression can be used to choose an option inside a DropDown? 但这表示未找到元素...可以使用什么其他表达式来选择DropDown中的选项?

Also is it possible to do it like mentioned below 也可以像下面提到的那样做

driver.find_element_by_xpath("//*[@placeholder='Select Quota']").click() driver.find_element_by_xpath(“ // * [@ placeholder ='选择配额']”)。click()

followed by something else? 其次是其他?

This drop down has been construct using select and options tags. 该下拉列表是使用select和options标记构造的。 So select class should work. 所以选择类应该工作。

You can try with this code : 您可以尝试使用以下代码:

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

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

Imports you will have to do : 您必须执行的导入:

from selenium.webdriver.support.ui import Select

As the <select> element is a Angular element so you have to induce WebDriverWait for the <select> element to be clickable and utilizing the Select class you can use the following solution: 由于<select>元素是Angular元素,因此必须诱使WebDriverWait使<select> 元素可单击,并利用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")

Just need to wait for the LD element to be clickable first, like so... 只需等待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']")))

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

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