Skip to main content
 首页 » 编程设计

Python Selenium IE之单击 anchor 不会更改 URL

2025年05月04日12txw1958

尽可能短

我正在尝试使用带有 py.test 和 Selenium 的 Python 进行测试自动化。

我尝试了两种方法来通过单击 anchor 来更改 URL <a>元素,这两种方法似乎都不起作用 - URL 没有改变。

除了断言结果不正确之外,没有报告任何错误。

我要点击的元素是:

<li class="ng-scope" ng-repeat="crumb in bcCtrl.data"> 
<!-- ngIf: !$last --> 
<a class="ng-binding ng-scope" ng-if="!$last" ng-href="#/accounts/8" href="#/accounts/8">RC München</a> 
<!-- end ngIf: !$last --> 
<!-- ngIf: $last --> 
</li> 
<!-- end ngRepeat: crumb in bcCtrl.data --> 

到目前为止我检查了什么

  • 我能找到 anchor ,
  • 找到的 anchor 是正确的(具有正确的文本和 href)
  • (使用 JScript)在 anchor 上执行 .click() 确实会导致页面加载新 URL,就像用户操作一样

第一种方法

第一种方法是使用:

pre_last_breadcrumb_element = self.find_elements_by_locator(locators['breadcrumb_elements'])[-1] 
pre_last_breadcrumb_element.click() 

但这似乎什么也没做。

第二种方法

第二种方法是使用:

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

但是还是没有想要的结果。

我做错了什么?请帮助...


更多详情

我使用的代码是:

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 

这是结果:

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' 

请您参考如下方法:

如果您尝试了所有方法但没有成功,您可以尝试使用 WebDriver#execute_script()作为替代解决方案,您可以在所需元素上执行一些 JavaScript 代码以执行 click ,如下所示:-

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) 

警告:- JavaScript 注入(inject) HTMLElement.click() 不应在测试上下文中使用。它违背了测试的目的。首先是因为它不会像真正的 click(focus、blur、mousedown、mouseup...) 那样生成所有事件,其次因为它不能保证真正的用户可以与元素交互.

但有时由于设计或其他问题,这将是唯一的解决方案,因此您可以将其视为替代解决方案。