我有一个带有一行复选框的页面。每个复选框旁边都有一个名称。 复选框元素位于div标签内。 div标签有一个ID。 我正在尝试点击一个名为的“复选框”两个名称中必须包含“性别”
我的XPath是:
By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//span//label[contains(text(), "%s")]/preceding::input[1]' % flag_field)
%s的值必须出现在两个名称中
单击复选框的我的Selenium Python方法是:
def add_checkbox_tick_from_flags_tab_for_possible_matches(self, flag_field):
# params flag_field: The field the matches is going to be added for, e.g. Allow gender mismatch
try:
checkbox_flag = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//span//label[contains(text(), "%s")]/preceding::input[1]' % flag_field)))
self.scroll_element_into_view(checkbox_flag)
if not checkbox_flag.is_selected(): # if not clicked, click it
checkbox_flag.click()
except NoSuchElementException, e:
self.save_screenshot("add_checkbox_tick_from_flags_tab_for_possible_matches")
raise
return True
从我的TestCase类中,我调用传递参数的方法,如下所示:
flags_tab.add_checkbox_tick_from_flags_tab_for_possible_matches("Gender must be present in both names")
我的基类中的get_element方法是:
# returns the element if found
def get_element(self, how, what):
# params how: By locator type
# params what: locator value
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print "Element not found "
print e
screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time. This way the screenshot name will be unique and be able to save
self.save_screenshot(screenshot_name)
raise
return element
HTML代码段(我删除了一些span标记,否则粘贴时间太长):
<div id="match_configuration_add_possible_tab_match_rules_fp_flags">
<div class="gwt-Label matchruleheader">Gender and title flags</div>
<span class="gwt-CheckBox" style="display: block;">
<input id="gwt-uid-268" type="checkbox" value="on" tabindex="0"/>
<label for="gwt-uid-268">Gender must be present in both names</label>
</span>
<span class="gwt-CheckBox" style="display: block;">
<input id="gwt-uid-269" type="checkbox" value="on" tabindex="0"/>
<label for="gwt-uid-269">Gender must be consistent in both names</label>
</span>
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
错误跟踪是:
Error
Traceback (most recent call last):
File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\TestCases\PossiblesPage_TestCase.py", line 213, in test_00009_add_possibles_match_rules
flags_tab.add_checkbox_tick_from_flags_tab_for_possible_matches("Gender must be present in both names")
File "C:\Webdriver\ClearCoreRegression Test\ClearCore - Regression Test\Pages\flags_tab.py", line 197, in add_checkbox_tick_from_flags_tab_for_possible_matches
checkbox_flag = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//span//label[contains(text(), "%s")]/preceding::input[1]' % flag_field)))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 187, in __call__
element = visibility_of_element_located(self.locator)(driver)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 73, in __call__
return _element_if_visible(_find_element(driver, self.locator))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 274, in _find_element
return driver.find_element(*by)
TypeError: 'WebElement' object is not callable
如果我运行以下代码,勾选所有复选框,则此代码有效。
try:
checkbox_element = self.driver.find_elements(By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//input')
for checkbox in checkbox_element:
if not checkbox.is_selected():
checkbox.click() # to tick it
return self
except NoSuchElementException, e:
它不会勾选我想要的单个复选框。奇怪。
我的XPATH看起来很正确。在Firebug XPATH检查器中,它会突出显示正确的复选框。 如何绕过错误对象不可调用?
以下是更全面的代码:
from Locators import Globals
from Menus.project_navigator import ProjectNavigatorPage
from Menus.toolbar import ToolbarPage
from Menus.Menu import MenuBarPage
from Pages.flags_tab import FlagsTab
from Base.BaseTestCase import BaseTestCase
from HTMLTestRunner2 import HTMLTestRunner
class PossiblesnPage_TestCase(BaseTestCase):
def test_00009_add_possibles_match_rules(self):
print "*** test_00009_add_possibles_match_rules *** "
project_navigator = ProjectNavigatorPage(self.driver)
possibles_page = project_navigator.select_projectNavigator_item("Possibles")
possibles_page.click_add_possibles_button()
possibles_page.enter_possibles_name_from_details_tab("Possibles")
possibles_page.enter_possibles_description_from_details_tab("Possibles description")
possibles_match_rules_tab = possibles_page.click_possibles_match_rules_tab()
possibles_match_rules_tab.click_possibles_match_rules_add_button()
possibles_match_rules_tab.enter_possibles_match_rule_name("name_addr")
possibles_match_rules_tab.click_selected_rule_radio_button_possibles("Name")
possibles_match_rules_tab.click_selected_rule_checkbox_possibles("Name")
flags_tab = FlagsTab(self.driver)
flags_tab.remove_ticks_from_all_checkboxes_possibles()
flags_tab.add_checkbox_tick_from_flags_tab_for_possible_matches("Gender must be present in both names")
if __name__ == "__main__":
#unittest.main()
HTMLTestRunner.main()
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from Locators.locators import MainPageLocators
from Locators.element import BasePageElement
from Pages.base import BasePage
from Pages.data_objects_saved_page import data_objects_saved_page
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
class FlagsTab(BasePage):
def __init__(self, d):
super(FlagsTab, self).__init__(d)
def add_checkbox_tick_from_flags_tab_for_possible_matches(self, flag_field):
# params flag_field: The field the matches is going to be added for, e.g. Allow gender mismatch
try:
checkbox_flag = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//span//label[contains(text(), "%s")]/preceding::input[1]' % flag_field)))
self.scroll_element_into_view(checkbox_flag)
if not checkbox_flag.is_selected():
checkbox_flag.click()
except NoSuchElementException, e:
print "Element not found %s " + e % flag_field
print e
self.save_screenshot("add_checkbox_tick_from_flags_tab_for_possible_matches")
return False
return True
from selenium.common.exceptions import TimeoutException
from Locators import Globals
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from Locators.locators import MainPageLocators
#from Utilities.HelperMethods import UsefulHelperMethods
from Utilities.HelperMethods import get_datetime_now
from selenium.webdriver.common.action_chains import ActionChains
class BasePage(object):
def __init__(self, driver):
self.driver = driver
self.driver.execute_script("window.onblur = function() { window.onfocus() }")
self.driver.implicitly_wait(120)
# returns the element if found
def get_element(self, how, what):
# params how: By locator type
# params what: locator value
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print "Element not found "
print e
screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time. This way the screenshot name will be unique and be able to save
self.save_screenshot(screenshot_name)
raise
return element
谢谢Riaz