簡體   English   中英

使用 Selenium python 選擇下拉框選項。 ng 選擇組合框 XPath

[英]Select dropdown box option using Selenium python. ng select combobox XPath

使用 python 和 selenium 在組合框下拉 ng select 中選擇一個選項。 我想在下拉選擇中選擇“男性”選項。 我正在嘗試使用 XPath。

我使用的代碼:

driver.find_element_by_id("sex_0").click()
driver.find_element_by_xpath("//*[contains(text(), 'MALE')]").click()

復制並粘貼 HTML:

請選擇選項女性

檢查 HTML 的元素:

<ng-select _ngcontent-uau-c14="" bindlabel="cdDescr" bindvalue="cd" class="custom ng-select ng-select-single ng-select-searchable ng-untouched ng-pristine ng-valid ng-select-opened ng-select-bottom" placeholder="Please Select Option" role="listbox" title="Please Select Option" id="sex_0">
 <div class="ng-select-container">
  <div class="ng-value-container"><div class="ng-placeholder">Please Select Option</div><!----><!---->
   <div class="ng-input" style="top: 0px;"><input role="combobox" type="text" autocomplete="ac08684b8af8" autocorrect="off" autocapitalize="off" aria-expanded="true" style="position: relative; top: 0px; left: -10px; padding-left: 10px; border: none !important; height: 45px; width: 468px;" aria-owns="ac08684b8af8" aria-activedescendant="ac08684b8af8-0"></div></div><!----><!---->
  <span class="ng-arrow-wrapper"><span class="ng-arrow"></span></span></div><!---->
 <ng-dropdown-panel class="ng-dropdown-panel ng-select-bottom" id="ac08684b8af8" style="opacity: 1;"><!---->
  <div class="ng-dropdown-panel-items scroll-host"><div></div>
   <div><!----><!---->
    <div class="ng-option ng-option-marked" role="option" aria-selected="false" id="ac08684b8af8-0"><!----><!----><span class="ng-option-label">FEMALE</span></div>
    <div class="ng-option" role="option" aria-selected="false" id="ac08684b8af8-1"><!----><!----><span class="ng-option-label">MALE</span></div>
<!----><!----><!----><!----></div></div><!----></ng-dropdown-panel></ng-select>

問題:

它將選擇 FEMALE 而不是 MALE,因為 XPath 文本“FEMALE”包含文本“MALE”。 我假設 'id="ac08684b8af8"' 是每次使用時隨機生成的,這意味着我不能使用 Selenium 通過 id 查找元素。 我嘗試使用 selenium 進行選擇,但出現錯誤:“ng select 類型的元素未選擇”。 因此我使用了 XPath。 當光標懸停在下拉選擇上方時,“ng-option-marked”只是一個功能。

我應該如何解決這個問題?

答案在 KunduK 的評論中。

driver.find_element_by_xpath("//*[text()='MALE']").click() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM