Python 2.7 Selenium Webdriver Python单击单选按钮显示WebElement对象不可调用
我正在尝试单击一个网页上的单选按钮,我正在使用SeleniumWebDriverPython自动化该网页。 当我的代码尝试单击单选按钮时,它显示错误:Python 2.7 Selenium Webdriver Python单击单选按钮显示WebElement对象不可调用,python-2.7,selenium,selenium-webdriver,Python 2.7,Selenium,Selenium Webdriver,我正在尝试单击一个网页上的单选按钮,我正在使用SeleniumWebDriverPython自动化该网页。 当我的代码尝试单击单选按钮时,它显示错误: TypeError:“WebElement”对象不可调用: 完全错误是: Traceback (most recent call last): File "C:\Webdriver\ClearCore\TestCases\MatchConfigrationPage_TestCase.py", line 85, in test_add_matc
TypeError:“WebElement”对象不可调用: 完全错误是:
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\MatchConfigrationPage_TestCase.py", line 85, in test_add_match_configuration_possibles_name
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
File "C:\Webdriver\ClearCore\Pages\match_rules_tab.py", line 82, in click_selected_rule_radio_button
radio_button = self.driver.find_element(By.XPATH, '//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[@type="radio"]')
TypeError: 'WebElement' object is not callable
我可以在FirefoxXPathChecker中使用以下XPATH找到该按钮
//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[@type="radio"]
我调用按钮并单击的方法如下:
from selenium.webdriver.common.by import By
def click_selected_rule_radio_button(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
radio_button = self.driver.find_element(By.XPATH, '//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="%s" and contains(text(), "%s")]//ancestor::tr[1]//input[@type="radio"]' (name, name))
self.driver.execute_script("arguments[0].click()", radio_button)
return self
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
def test_add_match_configuration_possibles_name(self):
print "*** Test add Match Configuration Possibles - Name ***"
projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
possiblesPage = projectNavigator.select_projectNavigator_item("Possibles") # click Possibles from project navigator
possiblesPage.click_add_possibles_button()
possiblesPage.enter_possible_matches_name_and_description_from_details_tab("name_dob", "date of birth possible match rule")
possibles_match_rules_tab = possiblesPage.click_match_rules_tab()
possibles_match_rules_tab.click_possibles_match_rules_add_button()
possibles_match_rules_tab.enter_match_rule_name("name_dob")
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
方法中的name参数的值为“name”,代码中的%s的值为“name”
我还尝试了以下方法:
def click_selected_rule_radio_button2(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
#WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.ID, 'match_configuration_add_possible_tab_match_rules_ct_mapping_body')))
radio_button = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[@type="radio"]')))
radio_button.click()
return self
从我的TestCase类中,我调用该方法,如下所示:
from selenium.webdriver.common.by import By
def click_selected_rule_radio_button(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
radio_button = self.driver.find_element(By.XPATH, '//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="%s" and contains(text(), "%s")]//ancestor::tr[1]//input[@type="radio"]' (name, name))
self.driver.execute_script("arguments[0].click()", radio_button)
return self
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
def test_add_match_configuration_possibles_name(self):
print "*** Test add Match Configuration Possibles - Name ***"
projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
possiblesPage = projectNavigator.select_projectNavigator_item("Possibles") # click Possibles from project navigator
possiblesPage.click_add_possibles_button()
possiblesPage.enter_possible_matches_name_and_description_from_details_tab("name_dob", "date of birth possible match rule")
possibles_match_rules_tab = possiblesPage.click_match_rules_tab()
possibles_match_rules_tab.click_possibles_match_rules_add_button()
possibles_match_rules_tab.enter_match_rule_name("name_dob")
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
测试用例的代码段如下:
from selenium.webdriver.common.by import By
def click_selected_rule_radio_button(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
radio_button = self.driver.find_element(By.XPATH, '//table[@id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[@title="%s" and contains(text(), "%s")]//ancestor::tr[1]//input[@type="radio"]' (name, name))
self.driver.execute_script("arguments[0].click()", radio_button)
return self
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
def test_add_match_configuration_possibles_name(self):
print "*** Test add Match Configuration Possibles - Name ***"
projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
possiblesPage = projectNavigator.select_projectNavigator_item("Possibles") # click Possibles from project navigator
possiblesPage.click_add_possibles_button()
possiblesPage.enter_possible_matches_name_and_description_from_details_tab("name_dob", "date of birth possible match rule")
possibles_match_rules_tab = possiblesPage.click_match_rules_tab()
possibles_match_rules_tab.click_possibles_match_rules_add_button()
possibles_match_rules_tab.enter_match_rule_name("name_dob")
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
HTML是:
<table id="match_configuration_add_possible_tab_match_rules_ct_mapping_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GPI5XK1CFG" __gwt_subrow="0" __gwt_row="0">
<td class="GPI5XK1CEG GPI5XK1CGG GPI5XK1CHG">
<div __gwt_cell="cell-gwt-uid-339" style="outline-style:none;" tabindex="0">
<input type="radio" name="rbCrossRow2" />
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CGG">
<div __gwt_cell="cell-gwt-uid-340" style="outline-style:none;">
<span class="" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span>
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CGG GPI5XK1CBH">
<div __gwt_cell="cell-gwt-uid-341" style="outline-style:none;">
<input id="match_configuration_add_possible_tab_match_rules_cb_name" type="checkbox" />
</div>
</td>
</tr>
<tr class="GPI5XK1CEH" __gwt_subrow="0" __gwt_row="1">
<td class="GPI5XK1CEG GPI5XK1CFH GPI5XK1CHG">
<div __gwt_cell="cell-gwt-uid-339" style="outline-style:none;">
<input type="radio" name="rbCrossRow2" />
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CFH">
<div __gwt_cell="cell-gwt-uid-340" style="outline-style:none;">
<span class="" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Address</span>
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CFH GPI5XK1CBH">
</tr>
<tr class="GPI5XK1CFG" __gwt_subrow="0" __gwt_row="2">
<tr class="GPI5XK1CEH" __gwt_subrow="0" __gwt_row="3">
</tbody>
名称
地址
任何人都可以看到什么是错误的,为什么单选按钮是不可调用的,它不会点击它
谢谢,
Riaz您可以使用JavaScript单击单选按钮。代码如下:
driver=webdriver.Chrome('./chromedriver.exe')
driver.get('your URL')
time.sleep(10)
radioButton = driver.find_element_by_xpath("Radio Button Xpath") #like //input[@id='female']
driver.execute_script("arguments[0].click();", radioButton)
#driver.quit()
如果你看我的问题,我已经试过JavaScript代码了。