问题
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'))
Result:
17
ko972
ko973
ko974
ko975
ko976
ko977
ko978
ko979
ko980
ko981
ko982
ko983
ko984
ko985
ko986
ko987
--> 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>
attempt 2:
options=browserdriver.find_elements_by_xpath("//*[@id='Xuất xứ']/option")
print(len(options))
for item in options:
print(item.text)
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
Some of my readings: 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
回答1:
Consider using innerText property
for item in select.options:
print(item.get_attribute('innerText'), item.get_attribute('value'))
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.
More information: Page Objects
回答2:
To print the text value of options.Use the property value innerHTML
options=browserdriver.find_elements_by_xpath("//*[@id='Xuất xứ']/option")
print(len(options))
for item in options:
print(item.get_attribute('innerHTML'))
Output:
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
来源:https://stackoverflow.com/questions/56644605/unable-to-print-text-of-options-in-select-in-selenium-python