Python Selenium - iterate through search results
<p>In the search results, I need to verify that all of them must contain the search key. This is the HTML source code:</p>

<pre><code>&lt;div id="content"&gt;
    &lt;h1&gt;Search Results&lt;/h1&gt;
    &lt;a id="main-content" tabindex="-1"&gt;&lt;/a&gt;     
        &lt;ul class="results"&gt;

                &lt;li&gt;&lt;img alt="Icon for Metropolitan trains" title="Metropolitan trains" src="themes/transport-site/images/jp/iconTrain.png" class="resultIcon"/&gt; &lt;strong&gt;Stop&lt;/strong&gt; &lt;a href="/next5/diva/10001218/1"&gt;Sunshine Railway Station (Sunshine)&lt;/a&gt;&lt;/li&gt;

                &lt;li&gt;&lt;img alt="Icon for Metropolitan trains" title="Metropolitan trains" src="themes/transport-site/images/jp/iconTrain.png" class="resultIcon"/&gt; &lt;strong&gt;Stop&lt;/strong&gt; &lt;a href="/next5/diva/10001003/1"&gt;Albion Railway Station (Sunshine North)&lt;/a&gt;&lt;/li&gt;                
        &lt;/ul&gt;       
&lt;/div&gt;
</code></pre>

<p>I have written this code to enter search key and get the results but it fails to loop through the search result:</p>

<pre><code>from selenium import webdriver

driver = webdriver.Chrome('C:/Users/lovea/OneDrive/Documents/Semester 2 2016/ISYS1087/w3-4/chromedriver')

driver.get('http://www.ptv.vic.gov.au')

next5Element = driver.find_element_by_link_text('Next 5 departures')
next5Element.click()

searchBox = driver.find_element_by_id('Form_ModeSearchForm_Search')
searchBox.click()
searchBox.clear()
searchBox.send_keys('Sunshine')

submitBtn = driver.find_element_by_id('Form_ModeSearchForm_action_doModeSearch')
submitBtn.click()

assert "Sorry, there were no results for your search." not in driver.page_source

results = driver.find_elements_by_xpath("//ul[@class='results']/li/a")
for result in results:
    assert "Sunshine" in result //Error: argument of type 'WebElement' is not iterable
</code></pre>

<p>Anyone please tell me what is the proper way to to that? Thank you!</p>

<p>I got a mistake in <code>assert</code> statement.</p>

<p>Because <code>result</code> is a WebElement so there is no text to look up in it.</p>

<p>I just change it like: <code>assert "Sunshine" in result.text</code></p>

python selenium selenium-webdriver