【问题标题】:Unable to print text of options in select in selenium python无法在 selenium python 中打印选择中的选项文本
【发布时间】:2019-06-18 08:13:29
【问题描述】:

我尝试打印选择元素中所有选项的文本。但结果只是空字符串,尽管该列表的 len 为真。 我已经阅读了其他一些类似的问题,但没有解决方案适合我。 这是我的一些尝试: 尝试 1:

select = UI.Select(browserdriver.find_element_by_xpath("//select[@id='Xuất xứ']"))
#select = Select(browserdriver.find_element_by_xpath("//select[@id='Xuất xứ']"))
options = select.options
print(len(options))
for item in options:
    print(item.text, item.get_attribute('value'))

结果:

17

 ko972
 ko973
 ko974
 ko975
 ko976
 ko977
 ko978
 ko979
 ko980
 ko981
 ko982
 ko983
 ko984
 ko985
 ko986
 ko987

--> 表示 xpath 为真,我可以到达那些选项的 value 属性。 17 是选项的长度。但 item.text 返回 null 这是该选择元素的 html:

<select class="form-control  selectized" data-bind="selectize:SelectedAtt, selectizeOptions:AttributeValues, optionsText: 'Value', value: SelectedAtt, enable: $parent.ProductDetail().CanEdit, optionsCaption: 'Vui lòng chọn'
                                 , attr: {id: $data.Name() }
                                " tabindex="-1" id="Xuất xứ" style="display: none;">
<option value="">Vui lòng chọn</option>
<option value="ko947">Hàn Quốc</option>
<option value="ko948">Mỹ</option>
<option value="ko949">Anh</option>
<option value="ko950">Pháp</option>
<option value="ko951">Thụy Sỹ</option>
<option value="ko952">Nga</option>
<option value="ko953">Na Uy</option>
<option value="ko954">Nhật</option>
<option value="ko955">Đài Loan</option>
<option value="ko956">Trung Quốc</option>
<option value="ko957">indonesia</option>
<option value="ko958">Singapore</option>
<option value="ko959">Malaysia</option>
<option value="ko960">Khác</option>
<option value="ko961">Việt nam</option>
<option value="ko962">Việt Nam</option>
</select>

尝试2:

options=browserdriver.find_elements_by_xpath("//*[@id='Xuất xứ']/option")
print(len(options))
for item in options:
     print(item.text)

结果与返回选项的真正 len 相同,但该列表中的每个项目都是空的。 尝试2的结果:

17

















请分享我解决此问题的方法。我想获得这些选项的文本。 非常感谢

我的一些读物: https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver/2258#2258

Listing select option values with Selenium and Python

How to print the text of the selected option choosen through 'selectByVisibleText' method in selenium

【问题讨论】:

  • 你能分享第二次尝试的结果(打印)吗?
  • 是的,已添加。我还在尝试 2 代码中编辑了我的拼写错误(将 get_elements_by_xpath 更改为 find_elements_by_xpath)

标签: python-3.x selenium select option


【解决方案1】:

考虑使用innerText property

for item in select.options:
    print(item.get_attribute('innerText'), item.get_attribute('value'))

如果您考虑Page Object Model 设计模式,这也可能是有益的,它将允许您将测试逻辑从 DOM 中分离出来,并且测试开发过程,尤其是支持/修复将更快、更容易。

更多信息:Page Objects

【讨论】:

  • 太棒了!这完全符合我的预期。非常感谢您提供此结果以及阅读 POM 建议。谢谢!!!!
  • 如果可能的话,请看看这个问题,stackoverflow.com/questions/56625545/…
【解决方案2】:

打印options的文本值。使用属性值innerHTML

options=browserdriver.find_elements_by_xpath("//*[@id='Xuất xứ']/option")
print(len(options))
for item in options:
     print(item.get_attribute('innerHTML'))

输出:

17
Vui lòng chọn
Hàn Quốc
Mỹ
Anh
Pháp
Thụy Sỹ
Nga
Na Uy
Nhật
Đài Loan
Trung Quốc
indonesia
Singapore
Malaysia
Khác
Việt nam
Việt Nam

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2018-01-24
    相关资源
    最近更新 更多