Skip to content

Commit

Permalink
Merge pull request #286 from kedder/master
Browse files Browse the repository at this point in the history
More robust find_link_by_partial_text()
  • Loading branch information
andrewsmedina committed Jan 24, 2014
2 parents e607a53 + a74fb72 commit 9bcaebe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion splinter/driver/webdriver/__init__.py
Expand Up @@ -180,7 +180,7 @@ def find_link_by_partial_href(self, partial_href):
return self.find_by_xpath('//a[contains(@href, "%s")]' % partial_href, original_find="link by partial href", original_query=partial_href)

def find_link_by_partial_text(self, partial_text):
return self.find_by_xpath('//a[contains(text(), "%s")]' % partial_text, original_find="link by partial text", original_query=partial_text)
return self.find_by_xpath('//a[contains(normalize-space(.), "%s")]' % partial_text, original_find="link by partial text", original_query=partial_text)

def find_link_by_text(self, text):
return self.find_by_xpath('//a[text()="%s"]' % text, original_find="link by text", original_query=text)
Expand Down
2 changes: 1 addition & 1 deletion splinter/driver/zopetestbrowser.py
Expand Up @@ -164,7 +164,7 @@ def find_link_by_partial_href(self, partial_href):
return self._find_links_by_xpath("//a[contains(@href, '%s')]" % partial_href)

def find_link_by_partial_text(self, partial_text):
return self._find_links_by_xpath("//a[contains(text(), '%s')]" % partial_text)
return self._find_links_by_xpath("//a[contains(normalize-space(.), '%s')]" % partial_text)

def fill(self, name, value):
self.find_by_name(name=name).first._control.value = value
Expand Down
4 changes: 4 additions & 0 deletions tests/find_elements.py
Expand Up @@ -86,6 +86,10 @@ def test_finding_all_links_by_partial_text(self):
link = self.browser.find_link_by_partial_text('FOO')[0]
self.assertEqual('http://localhost:5000/foo', link['href'])

def test_finding_all_links_by_partial_text_complex_contents(self):
link = self.browser.find_link_by_partial_text('Complex Link')[0]
self.assertEqual('http://localhost:5000/foo', link['href'])

def test_finding_last_element_by_css(self):
"should find last element by css"
value = self.browser.find_by_css('h1').last.value
Expand Down
6 changes: 6 additions & 0 deletions tests/static/index.html
Expand Up @@ -108,6 +108,12 @@ <h1 id="firstheader">Example Last Header</h1>
<div id="simple_text">my test text</div>
<div id="text_with_html">another <b>b</b>it of text</div>
<a href="http://localhost:5000/foo">FOO</a>
<a href="http://localhost:5000/foo">
<i class="fa fa-fw fa-user"></i>
&nbsp;
<span>Complex</span>
<span style="display: inline;" class="env-DEV">Link</span>
</a>
<a href="http://localhost:5000/foo">A wordier (and last) link to FOO</a>
<a href="http://localhost:5000/bar">BAR: <span>first bar</span></a>
<a href="http://localhost:5000/bar">BAR: <span>second bar</span></a>
Expand Down

0 comments on commit 9bcaebe

Please sign in to comment.