Python Selenium IE - 单击锚点不会更改 URL
Python Selenium IE - Clicking on an anchor does not change the URL
越短越好
我正在尝试使用 Python 与 py.test 和 Selenium 进行测试自动化。
我尝试了两种方法来通过单击锚点 <a>
元素来更改 URL,这两种方法似乎都不起作用 - 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 -->
到目前为止我检查了什么
- 我能找到主播,
- 找到的锚是正确的(具有正确的文本和 href)
- (使用 JScript)在锚点上执行 .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
注入 HTMLElement.click()
不应在测试上下文中使用。它违背了测试的目的。首先是因为它不会像真实的 click (focus, blur, mousedown, mouseup...)
那样生成所有事件,其次是因为它不能保证真实的用户可以与该元素进行交互。
但有时由于设计或其他问题,这将是唯一的解决方案,因此您可以将其视为替代解决方案。
越短越好
我正在尝试使用 Python 与 py.test 和 Selenium 进行测试自动化。
我尝试了两种方法来通过单击锚点 <a>
元素来更改 URL,这两种方法似乎都不起作用 - 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 -->
到目前为止我检查了什么
- 我能找到主播,
- 找到的锚是正确的(具有正确的文本和 href)
- (使用 JScript)在锚点上执行 .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
注入 HTMLElement.click()
不应在测试上下文中使用。它违背了测试的目的。首先是因为它不会像真实的 click (focus, blur, mousedown, mouseup...)
那样生成所有事件,其次是因为它不能保证真实的用户可以与该元素进行交互。
但有时由于设计或其他问题,这将是唯一的解决方案,因此您可以将其视为替代解决方案。