使用Selenium python选择下拉框选项。ng选择组合框XPath

使用Selenium python选择下拉框选项。ng选择组合框XPath,python,html,css,selenium,xpath,Python,Html,Css,Selenium,Xpath,使用python和selenium在组合框下拉列表ng select中选择一个选项。我想在下拉选择中选择“男性”选项。我正在尝试使用XPath 我使用的代码是: driver.find_element_by_id("sex_0").click() driver.find_element_by_xpath("//*[contains(text(), 'MALE')]").click() 复制并粘贴HTML: <ng-select _ngcontent-uau-c14="" bindlabe

使用python和selenium在组合框下拉列表ng select中选择一个选项。我想在下拉选择中选择“男性”选项。我正在尝试使用XPath

我使用的代码是:

driver.find_element_by_id("sex_0").click()
driver.find_element_by_xpath("//*[contains(text(), 'MALE')]").click()
复制并粘贴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>
请选择选项女性男性

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

请选择选项
女性
男性
问题是:

它将选择女性而不是男性,因为XPath文本“女性”包含文本“男性”。我假设每次使用都会随机生成“id=”ac08684b8af8“,这意味着我无法使用Selenium按id查找元素。我尝试使用Selenium进行选择,但出现错误:“ng类型的元素选择不选择”。因此我使用了XPath。当光标悬停在下拉选择上方时,“标记的ng选项”只是一个功能


我应该如何解决这个问题?

答案在昆都克的评论中

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

使用此
驱动程序代替contains。通过xpath(“/*[text()='MALE']”查找元素。单击()