无法在硒Python中选择中打印选项的文本 [英] Unable to print text of options in select in selenium python

查看:68
本文介绍了无法在硒Python中选择中打印选项的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试打印出select元素中所有选项的文本.但是,尽管该列表的len为true,但结果只是空字符串. 我读过其他一些类似的问题,但没有解决方法适合我. 这是我的一些尝试: 尝试1:

I try to print out text of all options in select element. But the result is just null string despite the len of that list is true. I have read some other similar questions but no soltion works for me. here are some of my attempts: Attempt 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为true,我可以到达这些选项的value属性. 17是选项的镜头.但是item.text返回null 这是该select元素的html:

--> It means that the xpath is true and I can reach the value attribute of those options. 17 is the len of options. But item.text returns null Here is the html of that select element:

<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相同,但该列表中的每个项目均为null. 尝试2的结果:

The result is the same that it returns the true len of options but each item in that list is null. Result of attempt 2:

17

















请给我分享这个问题的解决方案.我想获取这些选项的文本. 非常感谢

Pls, share me solution for this problem. I want to get text of those options. thanks alot

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

列出带有Selenium和Python的选择选项值

如何打印文本硒中通过"selectByVisibleText"方法选择的所选选项的选择

推荐答案

考虑使用 innerText财产

Consider using innerText property

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

如果您考虑页面对象,这可能也会有所帮助模型设计模式,它将使您能够将测试逻辑与DOM分离开来,并且测试开发过程(尤其是支持/修复过程)将变得更快,更容易.

It might be also beneficial if you consider Page Object Model design pattern, it will allow you to split test logic from DOM and the process of tests development and especially support/fixing will be faster and easier.

更多信息:页面对象

这篇关于无法在硒Python中选择中打印选项的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆