Python Selenium IE - Clicking on an anchor does not change the URL
<h2>As Short As Possible</h2>

<p>I am trying to do test automation using Python with py.test and Selenium.</p>

<p>I have tried two approaches to change the URL by clicking an anchor <code>&lt;a&gt;</code> element, neither of the approaches seems to work - the URL does not change.</p>

<p>There are no errors reported, other than the incorrect assertion result.</p>

<p>The element I want to click on is:</p>

<pre><code>&lt;li class="ng-scope" ng-repeat="crumb in bcCtrl.data"&gt;
&lt;!-- ngIf: !$last --&gt;
&lt;a class="ng-binding ng-scope" ng-if="!$last" ng-href="#/accounts/8" href="#/accounts/8"&gt;RC MÃ¼nchen&lt;/a&gt;
&lt;!-- end ngIf: !$last --&gt;
&lt;!-- ngIf: $last --&gt;
&lt;/li&gt;
&lt;!-- end ngRepeat: crumb in bcCtrl.data --&gt;
</code></pre>

<h3>What I have checked so far</h3>

<ul>
<li>I can find the anchor,</li>
<li>The anchor found is the correct one (has the correct text, and href)</li>
<li>(using JScript) That doing .click() on the anchor indeed causes the page to load the new URL just like the user action would</li>
</ul>

<h3>First approach</h3>

<p>The first approach was to use:</p>

<pre><code>pre_last_breadcrumb_element = self.find_elements_by_locator(locators['breadcrumb_elements'])[-1]
pre_last_breadcrumb_element.click()
</code></pre>

<p>but this seems not to have done anything.</p>

<h3>Second approach</h3>

<p>The second approach was to use:</p>

<pre><code>actions = ActionChains(self.driver)
el = self.find_elements_by_locator(locators['breadcrumb_elements'])[-1]
actions.move_to_element(el).click(el).perform()
</code></pre>

<p>But still no desired result.</p>

<p>What am I doing wrong? Please help...</p>

<hr>

<h2>More details</h2>

<p>The code that I've used is:</p>

<pre><code>pre_last_breadcrumb_element = self.find_elements_by_locator(locators['breadcrumb_elements'])[-1]
# this will click the pre-last element in the breadcrumb
print("PreLast = {0}".format(pre_last_breadcrumb_element.text))
print("BEFORE CLICK get_current_url='{0}'".format(self.driver.get_current_url))

# APPROACH 1
actions = ActionChains(self.driver)
el = self.find_elements_by_locator(locators['breadcrumb_elements'])[-1]
actions.move_to_element(el).click(el).perform()
# FIXME: The click on the anchor does not work...

# APPROACH 2
# pre_last_breadcrumb_element.click()
# FIXME: The click on the anchor does not work...

print("AFTER  CLICK get_current_url='{0}'".format(self.driver.get_current_url))
# checking if the pre-last element changed due to the click
print("PreLast = {0}".format(pre_last_breadcrumb_element.text))
print("Changing between URLs\nFROM:'{0}'\n  TO:'{1}'".format(self.get_url, pre_last_breadcrumb_element.get_attribute("href")))

# this will reload the page class at the newer URL
self.open_at_current_location()
print("Current URL:'{0}'".format(self.get_url))
assert last_breadcrumb_element.text is pre_last_breadcrumb_element.text
</code></pre>

<p>This is the result:</p>

<pre><code>PreLast = RC MÃ¼nchen
BEFORE CLICK get_current_url='http://fct:8080/fct/#/accounts/9'
AFTER  CLICK get_current_url='http://fct:8080/fct/#/accounts/9'
PreLast = RC MÃ¼nchen
Changing between URLs
FROM:'http://fct:8080/fct/#/accounts/9'
  TO:'http://fct:8080/fct/#/accounts/8'
get_current_url='http://fct:8080/fct/#/accounts/9'
Current URL:'http://fct:8080/fct/#/accounts/9'
</code></pre>

<p>If you've tried all but nothing get success, you can try using <a href="http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script" rel="nofollow"><code>WebDriver#execute_script()</code></a> as an alternate solution where you can execute some piece of <code>JavaScript</code> code on the desired element to perform <code>click</code> as below :-</p>

<pre><code>element = self.driver.find_element_by_link_text("RC MÃ¼nchen")

#now click on this element using JavaScript 
self.driver.execute_script("arguments[0].click()", element)
</code></pre>

<p><strong>Warning</strong> :- The <code>JavaScript</code> injection <code>HTMLElement.click()</code> shouldn't be used in a testing context. It defeats the purpose of the test. First because it doesn't generate all the events like a real <code>click (focus, blur, mousedown, mouseup...)</code> and second because it doesn't guarantee that a real user can interact with the element. </p>

<p>But some time due to designing or other issues this is to be only solution, so you can consider it as an alternate solution.</p>

python internet-explorer selenium-webdriver automated-tests anchor