我正在尝试点击我在Selenium Webdriver Python中自动化的网页上的单选按钮。
当我的代码试图点击单选按钮时,它显示错误:
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
我可以在Firefox XPATH检查器中使用以下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
方法中的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类中,我按如下方式调用该方法:
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>
任何人都可以看到有什么问题,为什么单选按钮不可调用,它不会点击它?
谢谢, 里亚兹
答案 0 :(得分:-1)
您可以使用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()