这是来自SAPUI。我在这个元素上尝试了各种各样的XPath。我已经在Chrome控制台上试用了它们,也验证了XPath是否正确。我甚至尝试过使用其他所有JavaScriptExecutor Actions,但是每次都无法点击它。
见下面的代码。我想点击引号。
我使用过的一些XPath:
hosts = ['host1', 'host2', 'host3']
ips = ['ip1', 'ip2', 'ip3']
assert len(hosts) == len(ips) # check that the 2 list are the same lenght
with open('devicelist.txt', 'w') as out_file:
for i in xrange(len(ips)):
out_file.write(hosts[i] + ' - ' + ips[i] + '\n')
他们中的每一个都返回此无法定位的元素。
PS:对不起,我不知道如何复制粘贴代码。当我使用4个空格技巧时,它就像一行代码一样。
添加了HTML代码:
//a[@id="navigationitemNAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c_4475"]
//a[contains(text(), "Quotes")]
//div[@role='Main']//li[@class='sapUiUx3NavBarItemSel']/a[contains(text(), 'Quotes')]
这是其中的亮点:
<nav id="__bar16" data-sap-ui="__bar16" data-sap-ui-fastnavgroup="true" data-flex-dragtype="FacetTab" data-flex-containerid="gTMZ5G7rXqQM1RsPGdQWHW" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" class="sapUiUx3NavBar sapUiUx3NavBarUpperCaseText sapUiUx3NavBarScrollForward" role="navigation" tabindex="0">
<ul id="__bar16-list" role="menubar" class="sapUiUx3NavBarList" style="white-space:nowrap">
<li style="display:inline-block"><a id="__bar16-dummyItem" class="sapUiUx3NavBarDummyItem sapUiUx3NavBarItem"> </a></li>
<li style="display:inline-block"><a id="navigationitemCcuq0IkG74_K2rh3LWQ_s4W_4473" data-sap-ui="navigationitemCcuq0IkG74_K2rh3LWQ_s4W_4473" data-flex-dragtype="FacetTab" data-flex-modelid="Ccuq0IkG74_K2rh3LWQ$4W" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="Ccuq0IkG74_K2rh3LWQ$4W" href="#" aria-setsize="18" aria-posinset="1" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Overview</a></li>
<li style="display:inline-block"><a id="navigationitemNCkvR0ZHXKgmQFks1tvHvG_4474" data-sap-ui="navigationitemNCkvR0ZHXKgmQFks1tvHvG_4474" data-flex-dragtype="FacetTab" data-flex-modelid="NCkvR0ZHXKgmQFks1tvHvG" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="NCkvR0ZHXKgmQFks1tvHvG" href="#" aria-setsize="18" aria-posinset="2" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Products</a></li>
<li style="display:inline-block" class="sapUiUx3NavBarItemSel"><a id="navigationitemNAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c_4475" data-sap-ui="navigationitemNAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c_4475" data-flex-dragtype="FacetTab" data-flex-modelid="NAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="NAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c" href="#" aria-setsize="18" aria-posinset="3" role="menuitemradio" class="sapUiUx3NavBarItem" tabindex="-1" aria-checked="true">Quotes</a></li>
<li style="display:inline-block"><a id="navigationitemHnYQXmNzpKsQ_svo9v_7Ucm_4476" data-sap-ui="navigationitemHnYQXmNzpKsQ_svo9v_7Ucm_4476" data-flex-dragtype="FacetTab" data-flex-modelid="HnYQXmNzpKsQ$vo9v_7Ucm" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="HnYQXmNzpKsQ$vo9v_7Ucm" href="#" aria-setsize="18" aria-posinset="4" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Feed</a></li>
<li style="display:inline-block"><a id="navigationitemAPb3M2Dc14oYbC9UnR2nmW_4479" data-sap-ui="navigationitemAPb3M2Dc14oYbC9UnR2nmW_4479" data-flex-dragtype="FacetTab" data-flex-modelid="APb3M2Dc14oYbC9UnR2nmW" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="APb3M2Dc14oYbC9UnR2nmW" href="#" aria-setsize="18" aria-posinset="6" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Sales Activities</a></li>
<li style="display:inline-block"><a id="navigationitem3c4XOx_s7j42mfRqSwAA2eW_4480" data-sap-ui="navigationitem3c4XOx_s7j42mfRqSwAA2eW_4480" data-flex-dragtype="FacetTab" data-flex-modelid="3c4XOx$7j42mfRqSwAA2eW" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="3c4XOx$7j42mfRqSwAA2eW" href="#" aria-setsize="18" aria-posinset="7" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Competitors</a></li>
<li style="display:inline-block"><a id="navigationitembnmA5f4XVK_1waGBR_2Bc0_4481" data-sap-ui="navigationitembnmA5f4XVK_1waGBR_2Bc0_4481" data-flex-dragtype="FacetTab" data-flex-modelid="bnmA5f4XVK_1waGBR_2Bc0" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="bnmA5f4XVK_1waGBR_2Bc0" href="#" aria-setsize="18" aria-posinset="8" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Sales Team</a></li>
<li style="display:inline-block"><a id="navigationitemFWZk1s_sFJKoD2aGe6F_saYW_4482" data-sap-ui="navigationitemFWZk1s_sFJKoD2aGe6F_saYW_4482" data-flex-dragtype="FacetTab" data-flex-modelid="FWZk1s$FJKoD2aGe6F$aYW" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="FWZk1s$FJKoD2aGe6F$aYW" href="#" aria-setsize="18" aria-posinset="9" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Contacts</a></li>
<li style="display:inline-block"><a id="navigationitem4jNgg9kvhKQGhD3t9kmXjm_4484" data-sap-ui="navigationitem4jNgg9kvhKQGhD3t9kmXjm_4484" data-flex-dragtype="FacetTab" data-flex-modelid="4jNgg9kvhKQGhD3t9kmXjm" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="4jNgg9kvhKQGhD3t9kmXjm" href="#" aria-setsize="18" aria-posinset="10" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Involved Parties</a></li>
<li style="display:inline-block"><a id="navigationitemw0SBWQJSI4cZoAZhFWnLsG_4488" data-sap-ui="navigationitemw0SBWQJSI4cZoAZhFWnLsG_4488" data-flex-dragtype="FacetTab" data-flex-modelid="w0SBWQJSI4cZoAZhFWnLsG" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="w0SBWQJSI4cZoAZhFWnLsG" href="#" aria-setsize="18" aria-posinset="12" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Timeline</a></li>
<li style="display:inline-block"><a id="navigationitemgJDHYohCDKEfyrkvqRzy7m_4489" data-sap-ui="navigationitemgJDHYohCDKEfyrkvqRzy7m_4489" data-flex-dragtype="FacetTab" data-flex-modelid="gJDHYohCDKEfyrkvqRzy7m" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="gJDHYohCDKEfyrkvqRzy7m" href="#" aria-setsize="18" aria-posinset="13" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Document Flow</a></li>
<li style="display:inline-block"><a id="navigationitemj1QuCToZNK6RuFGhGMCOvG_4492" data-sap-ui="navigationitemj1QuCToZNK6RuFGhGMCOvG_4492" data-flex-dragtype="FacetTab" data-flex-modelid="j1QuCToZNK6RuFGhGMCOvG" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="j1QuCToZNK6RuFGhGMCOvG" href="#" aria-setsize="18" aria-posinset="14" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Attachments</a></li>
<li style="display:inline-block"><a id="navigationitem0OeJe07pn4ACaAbX_s11Pz0_4497" data-sap-ui="navigationitem0OeJe07pn4ACaAbX_s11Pz0_4497" data-flex-dragtype="FacetTab" data-flex-modelid="0OeJe07pn4ACaAbX$11Pz0" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="0OeJe07pn4ACaAbX$11Pz0" href="#" aria-setsize="18" aria-posinset="15" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Changes</a></li>
<li style="display:inline-block"><a id="navigationitemNAVIGATIONITEMID_ea96ffcf719448e09193dab60180ef70_4500" data-sap-ui="navigationitemNAVIGATIONITEMID_ea96ffcf719448e09193dab60180ef70_4500" data-flex-dragtype="FacetTab" data-flex-modelid="NAVIGATIONITEMID_ea96ffcf719448e09193dab60180ef70" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="NAVIGATIONITEMID_ea96ffcf719448e09193dab60180ef70" href="#" aria-setsize="18" aria-posinset="16" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">Governance</a></li>
<li style="display:inline-block"><a id="navigationitem59721243ab594d8d89cf28be315a1e98_4501" data-sap-ui="navigationitem59721243ab594d8d89cf28be315a1e98_4501" data-flex-dragtype="FacetTab" data-flex-modelid="59721243ab594d8d89cf28be315a1e98" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="59721243ab594d8d89cf28be315a1e98" href="#" aria-setsize="18" aria-posinset="17" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">OPPORTUNITY PLANNING</a></li>
<li style="display:inline-block"><a id="navigationitem191194b28e914b10ad51961c568fbf2d_4502" data-sap-ui="navigationitem191194b28e914b10ad51961c568fbf2d_4502" data-flex-dragtype="FacetTab" data-flex-modelid="191194b28e914b10ad51961c568fbf2d" data-flex-component="/BYD_COD/SalesOnDemand/Opportunity/UI/COD_Opportunity_TI.TI.uicomponent&null" data-sap-automation-id="191194b28e914b10ad51961c568fbf2d" href="#" aria-setsize="18" aria-posinset="18" role="menuitemradio" class="sapUiUx3NavBarItem" aria-checked="false">PLANNING REPORTS</a></li><span id="__bar16-arrow" style="left: 290px;" class="sapUiUx3NavBarArrow"></span></ul>
<a id="__bar16-ofb" role="presentation" class="sapUiUx3NavBarBack" href="#"></a>
<a id="__bar16-off" role="presentation" class="sapUiUx3NavBarForward" href="#"></a><a id="__bar16-ofl" role="presentation" class="sapUiUx3NavBarOverflowBtn" href="#"><span class="sapUiIcon sapUiIconMirrorInRTL" style="font-family: SAP-icons;" data-sap-ui-icon-content="" role="presentation" title="More" id="__bar16-oflt" aria-label="More"></span></a></nav>
答案 0 :(得分:1)
当定位器正确时,它必须是时间问题。尝试使用明确的等待:
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(By.id("navigationitemNAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c_4518")));
我认为您使用Chrome开发者工具检查了定位器。请参阅:http://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/
答案 1 :(得分:0)
如果HTML元素绑定到HTML命名空间,那么您的XPath不会匹配。您可以尝试使用*
使用更通用的元素匹配。由于@id
应该是唯一的,因此以下内容应该足够了:
//*[@id='navigationitemNAVIGATIONITEMID_7bc2f6d569b74f2fa33d38fa9962135c_4518']
对于查找包含单词&#34;引号&#34;的元素的更通用标准,您也可以限制匹配元素&#39; s local-name()
:
//*[local-name()='a' and contains(text(),'Quotes')]
答案 2 :(得分:0)
您的方案中可能有两种情况可能会拒绝您找到..
dynamic id
更改。正如你前面提到的那样,你已经尝试了所有的东西......似乎元素在iframe里面。如果是这种情况,请使用以下方式找到它..
driver.switchTo().frame("iframe id");