I have a page with a row of checkboxes. Each checkbox has a name beside it. The checkbox elements are inside a div tag. The div tag has an ID. I am trying to click a checkbox which has the name "Gender must be present in both names"
My XPath is:
By.XPATH, '//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]//span//label[contains(text(), "%s")]/preceding::input[1]' % flag_field)
%s has the value Gender must be present in both names
My Selenium Python method to click the checkbox is:
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
From my TestCase class i call the method passing the parameter like this:
flags_tab.add_checkbox_tick_from_flags_tab_for_possible_matches("Gender must be present in both names")
My get_element method in my base class is:
# 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
The HTML snippet is (I have removed some span tags otherwise it will be too long to paste):
<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;">
The error trace is:
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
If i run the following code which ticks all of the checkboxes, this one works.
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:
It just won't tick an individual checkbox which i want. Strange.
My XPATH looks correct. In Firebug XPATH checker it does highlight the correct checkbox. How can i get around the error Object is not callable?
Here is more of the fuller code:
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
Thanks, Riaz