我正在尝试使用XPATH定位器单击包含文本“ Clean feed crm”的span标签。我得到错误:
UnboundLocalError: local variable 'element' referenced before assignment
完整的错误跟踪:
Traceback (most recent call last): File "C:\Webdriver\ClearCore\TestCases\OperationsPage_TestCase.py", line 56, in test_add_and_run_clean_process process_lists_page.click_clean_feed_task_from_groups_tab(Globals.process_lists_clean_feed_task_crm) File "C:\Webdriver\ClearCore\Pages\operations.py", line 90, in click_clean_feed_task_from_groups_tab clean_feed_crm_element = self.get_element(By.XPATH, '//div[@id="operations_add_process_list_ct_groups_and_tasks"]//../span[contains(text(), "Clean feed crm")]') File "C:\Webdriver\ClearCore 501\Pages\base.py", line 31, in get_element return element UnboundLocalError: local variable 'element' referenced before assignment
如果我使用绝对完整的XPATH,则效果很好。相对的XPATH显示错误。
可以使用的完整绝对XPATH是:
(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[7]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[3]/div/div[2]/div/div[1]/div/div/div/div/div[1]/div[1]/div[2]/div/div[1]/div[1]/div/div/div[2]/div/div[2]/span[1]/span')
相对的XPATH不起作用是:
(By.XPATH, '//div[@id="operations_add_process_list_ct_groups_and_tasks"]//../span[contains(text(), "Clean feed crm")]')
HTML是:
<div id="operations_add_process_list_ct_groups_and_tasks" class="GPI5XK1CDG" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true" role="tree"> <div style="overflow: hidden;"> <div> <div> <div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-expanded="true" aria-level="1"> <div class="GPI5XK1CIF GPI5XK1CAG" style="padding-left: 0px;"> <div style="overflow: hidden;"> <div> <div> <div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-level="2"> <div class="GPI5XK1CIF" style="padding-left: 16px;"> <div class="GPI5XK1CIF GPI5XK1CKF" style="padding-left: 16px;position:relative;" onclick=""> <div style="position:absolute;display:none;"/> <div class="GPI5XK1CLF"> <div style="padding-left: 22px;position:relative;zoom:1;"> <div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;"> <img border="0" style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC2UlEQVR42mNgQANlZ1PqG84XfA7YYVsG5AqBaK+lFnM0E5RUgXx2IGZkwAbq99ezgCTTj4Wu//jrw/+68/mf3BeZ7ei5XPdv6+M1/y2btBcC5TWghqCCwlPxzZ2Xqz/FHfbe3Xu57uudTzf+H3i+4//eZ1v/H3257//aB4v/a8XKrxE1Esw2LFDzBGrhAGImuAFdl2s+/fjz/f+r7y/+H3qxG6zx0ruz/8++Of5/x5P1/7c8Xv2/9lzu/7Izqf/9N9t8E9bidwZq44d5h9Fvo9XMlffn/z/x6iBY040Pl/8vuTvj/7bH68BiMENAdOnplP/SdqJTgfr0wd7xXmnu6DTTcOmcWxP+73m25f+tj9f+B+6w/afoJb5TNVRmHVDDl11PN/0HhQPQm//NazQ/sQuzgwxwB2IehsiDLr9AzgXZClJ49f0FYIDpXAVKLgHiCu81lis3PloBNgAYoP8NclVuc0lwTgbKOQAxN0PIPvuXR17u+X/wxS6wE0++PgyKgT9mNRpL/TZat0253v5m3cMl/0GGgLy56v6C/4YFqg+Bmr3B4WDdot3usdjsg+96K7AhIFeAYgCkCaQBRIP8D2JXns0AG6CdqHAXqLkYiGVBgSjMq8CdELjd9jvIAFDAgTDIyTA2SBPI/0CNT2UcRHfwynLNRTaAxahQrQ8U1yvuzQX7E2TrmgeLwDHRdKH4/9K7s/5Pu9H1X85VfC9Q/UwgzgVie2hUMjDopSs5A13wy3GywX+TMvXHwFj4DfJz4mG//1bNOv8yjoX+m3trIsjvT4HKa6FRyAfEzLC0xCnvKp4ibiI4CcieoREpe7DjUuV/0wqNn8yczEsF1XkXgdiiBoKrgPLlQCyHnidAHBEgtgRiJzlXiUagS35J24psAfJBhkYwczAnQjUHgsINW34CpW1OcOKAGOYBypxAHAXE8kAsBsSKUDkWBgIApEAISSMr1JWM6E4HAJKeit5kyDtvAAAAAElFTkSuQmCC) no-repeat 0px 0px;" src="http://justin-pc.infoshare.local:8080/clearcore501/ClearCore/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";"/> </div> <div> <span> <span class=" myinlineblock" title="Clean feed crm" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;margin-right:-14px;">Clean feed crm</span> </span> <span> <span class="" title="Turn task off or on." style=""> <input type="checkbox" checked="" tabindex="-1"/> </span> </span> </div> </div> </div> </div> </div> </div> <div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="2" aria-level="2"> <div class="GPI5XK1CIF" style="padding-left: 16px;"> <div class="GPI5XK1CIF GPI5XK1CKF" style="padding-left: 16px;position:relative;" onclick=""> <div style="position:absolute;display:none;"/> <div class="GPI5XK1CLF"> <div style="padding-left: 22px;position:relative;zoom:1;"> <div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;"> <img border="0" style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC2UlEQVR42mNgQANlZ1PqG84XfA7YYVsG5AqBaK+lFnM0E5RUgXx2IGZkwAbq99ezgCTTj4Wu//jrw/+68/mf3BeZ7ei5XPdv6+M1/y2btBcC5TWghqCCwlPxzZ2Xqz/FHfbe3Xu57uudTzf+H3i+4//eZ1v/H3257//aB4v/a8XKrxE1Esw2LFDzBGrhAGImuAFdl2s+/fjz/f+r7y/+H3qxG6zx0ruz/8++Of5/x5P1/7c8Xv2/9lzu/7Izqf/9N9t8E9bidwZq44d5h9Fvo9XMlffn/z/x6iBY040Pl/8vuTvj/7bH68BiMENAdOnplP/SdqJTgfr0wd7xXmnu6DTTcOmcWxP+73m25f+tj9f+B+6w/afoJb5TNVRmHVDDl11PN/0HhQPQm//NazQ/sQuzgwxwB2IehsiDLr9AzgXZClJ49f0FYIDpXAVKLgHiCu81lis3PloBNgAYoP8NclVuc0lwTgbKOQAxN0PIPvuXR17u+X/wxS6wE0++PgyKgT9mNRpL/TZat0253v5m3cMl/0GGgLy56v6C/4YFqg+Bmr3B4WDdot3usdjsg+96K7AhIFeAYgCkCaQBRIP8D2JXns0AG6CdqHAXqLkYiGVBgSjMq8CdELjd9jvIAFDAgTDIyTA2SBPI/0CNT2UcRHfwynLNRTaAxahQrQ8U1yvuzQX7E2TrmgeLwDHRdKH4/9K7s/5Pu9H1X85VfC9Q/UwgzgVie2hUMjDopSs5A13wy3GywX+TMvXHwFj4DfJz4mG//1bNOv8yjoX+m3trIsjvT4HKa6FRyAfEzLC0xCnvKp4ibiI4CcieoREpe7DjUuV/0wqNn8yczEsF1XkXgdiiBoKrgPLlQCyHnidAHBEgtgRiJzlXiUagS35J24psAfJBhkYwczAnQjUHgsINW34CpW1OcOKAGOYBypxAHAXE8kAsBsSKUDkWBgIApEAISSMr1JWM6E4HAJKeit5kyDtvAAAAAElFTkSuQmCC) no-repeat 0px 0px;" src="http://justin-pc.infoshare.local:8080/clearcore501/ClearCore/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";"/> </div> <div> <span> <span class=" myinlineblock" title="Clean feed escr" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;width:100%;margin-right:-14px;">Clean feed escr</span> </span> <span> <span class="" title="Turn task off or on." style=""> <input type="checkbox" checked="" tabindex="-1"/> </span> </span> </div> </div> </div> </div> </div> </div>
我的方法实现是:
def click_clean_feed_task_from_groups_tab(self, feed): # Params: feed: clean feed crm, clean feed escr or clean feed orchard #clean_feed_crm_element = self.driver.find_element(By.XPATH, '//span[@class="myinlineblock" and contains(text(), "%s") % feed]') clean_feed_crm_element = self.get_element(By.XPATH, '//div[@id="operations_add_process_list_ct_groups_and_tasks"]//../span[contains(text(), "Clean feed crm")]') #clean_feed_crm_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="operations_add_process_list_ct_groups_and_tasks"]//..//.//..//..//..//..//..//..//../span[contains(text(), "%s")] % feed ]'))) clean_feed_crm_element.click() return self
从我的TestCase类,我调用方法:
project_navigator = ProjectNavigatorPage(self.driver) process_lists_page = project_navigator.select_projectNavigator_item("Process Lists") process_lists_page.click_add_button_for_process_lists() process_lists_page.click_clean_task_arrow_to_expand_it_from_groups_tab("add") process_lists_page.click_clean_feed_task_from_groups_tab(Globals.process_lists_clean_feed_task_crm)
Globals.py是:
process_lists_clean_feed_task_crm = "Clean feed crm"
我也尝试过使用WebDriverWait仍然相同的错误:
clean_feed_crm_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(((By.XPATH, '//div[@id="operations_add_process_list_ct_groups_and_tasks"]//../span[contains(text(), "%s") % feed]')))
我正在查找此文本时,%s,%feed的值为“ Clean feed crm”(作为参数传递到我的方法中。
我究竟做错了什么?然后,我可以使用什么XPATH单击具有“ Clean feed crm”文本的元素?
谢谢,里亚兹
如果我们回想起XPath语法中的一些元素:
因此,当您编写:
//div[@id="operations_add_process_list_ct_groups_and_tasks"]//..
您正在选择div节点本身。从该节点,相对的XPath应该是:
//div[@id="operations_add_process_list_ct_groups_and_tasks"]//span[contains(text(), "Clean feed crm")]
这样,您可以选择具有选定ID的div节点,然后在内部查找包含文本的span标签。