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

我需要在嘗試使用的xpath的下拉列表中選擇一個值:

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

但這表示未找到元素...可以使用什么其他表達式來選擇DropDown中的選項?

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

driver.find_element_by_xpath(“ // * [@ placeholder ='選擇配額']”)。click()

其次是其他?

該下拉列表是使用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

由於<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")

只需等待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