Awad
Awad

Reputation: 5

I can't get all the results of element

I get a first text only (--chose--)

Why not print all the results (good_1, good_2,...) please What's the wrong here


drop_down = driver.find_elements_by_css_selector(".select2-container--default")[4]
ActionChains(driver).move_to_element(drop_down).perform()
drop_down.click()
time.sleep(1)
drop_down = driver.switch_to.active_element
element = driver.find_elements_by_css_selector(".select2-container--default")[4]
print(element.text)
all_options = drop_down.find_elements_by_tag_name("option")
for option in element:
    print(option.text)


This code same the problem:

element = driver.find_element_by_css_selector("#select2-ctl00_PlaceHolderMain_ddlPeriod-container")

HTML:





<div class="col-xs-12 no_padding" printable="true">
<div class="col-xs-5 col-sm-4 feild_title">
<span id="ctl00_PlaceHolderMain_tdPeriod" class="manditory">*</span>
<span id="ctl00_PlaceHolderMain_lblPeriodTitle" class="StandardFont" style="display:inline-block;width:100px;">test</span>
</div>
<div class="col-xs-7 col-sm-8 feild_data">
<select name="ctl00$PlaceHolderMain$ddlPeriod" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$PlaceHolderMain$ddlPeriod\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl00_PlaceHolderMain_ddlPeriod" class="control-dropdownlist" style="width:100%;">
<option selected="selected" value="-99">-- chose --</option>
<option value="200140">good_1</option>
<option value="200141">good_2</option>
<option value="200142">good_3</option>
<option value="200143">good_4</option>
</select>
<span id="ctl00_PlaceHolderMain_rfvPeriod" class="ValidationClass" style="display:inline-block;width:150px;display:none;">chose one.</span>
</div>
</div>


error:

-- chose --

Process finished with exit code -1073740791 (0xC0000409)

Upvotes: 0

Views: 80

Answers (2)

Dmitri T
Dmitri T

Reputation: 168122

You have a typo in your code:

Change this:

for option in element:
    print(option.text)

to this:

for option in all_options:
    print(option.text)

Also be informed that using time.sleep() is a some form of a performance anti-pattern, consider using Explicit Wait instead. Check out How to use Selenium to test web applications using AJAX technology article for comprehensive explanation and examples.

Upvotes: 0

frianH
frianH

Reputation: 7563

Try use xpath like bellow :

opts = driver.find_elements_by_xpath('//option[text() and ./parent::*[@id="ctl00_PlaceHolderMain_ddlPeriod"]]')
for opt in opts:
    print(opt.text)

Upvotes: 1

Related Questions