我使用下面的代码从列表中选择值,但它总是选择“Multan”我如何选择我选择的值如何获取索引并给出该特定索引
List <WebElement> myElements = driver.findElements(By.id("select2-district-results"));
String str1=myType.get(0).getText();
System.out.println("Type ----"+str1);
Thread.sleep(500);
myType.get(0).click();
我也使用了它,它给出了以下错误;
List <WebElement> myElements = driver.findElements(By.id("select2-district-results"));
Thread.sleep(500);
for(WebElement e : myElements) {
if(e.getText().equals("islamabad")) {
e.click();
return;
}
}
错误,我使用上面给出确切值的方法
org.openqa.selenium.WebDriverException: unknown error: Element <input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Select Commodities" style="width: 742px;"> is not clickable at point (851, 566). Other element would receive the click: <li class="select2-results__option" id="select2-district-result-6qcm-3" role="treeitem" aria-selected="false">...</li>
(Session info: chrome=60.0.3112.113)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 143 milliseconds
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'NISB-TEC-P4098', ip: '10.7.36.187', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\MADIHA~1.USM\AppData\Local\Temp\scoped_dir14228_17829}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: de41a07133958c4e9109dd6cd9135782
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
at com.sun.proxy.$Proxy7.click(Unknown Source)
at objectRepoWeb.AddAgent.ClickAssignCommodity(AddAgent.java:70)
at SourceSmartWeb.AddAgentWeb.AddNewAgent(AddAgentWeb.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
如何从列表中选择特定值,因为当我将索引更改为1时,它会给出数组超出范围的异常,它总是选择数字4的数字“Multan”列表我想选择我选择的值。< / p>
HTML:
<span class="select2-results">
<ul id="select2-district-results" class="select2-results__options" role="tree" aria-multiselectable="true" aria-expanded="true" aria-hidden="false">
<li id="select2-district-result-571b-1" class="select2-results__option" role="treeitem" aria-selected="false">islamabad</li>
<li id="select2-district-result-0mky-2" class="select2-results__option" role="treeitem" aria-selected="false">rawalpindi</li>
<li id="select2-district-result-m6x2-3" class="select2-results__option" role="treeitem" aria-selected="false">Karachi</li>
<li id="select2-district-result-ut6x-5" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Multan</li>
<li id="select2-district-result-8n3x-7" class="select2-results__option" role="treeitem" aria-selected="false">Jhelum</li>
<li id="select2-district-result-9hr4-8" class="select2-results__option" role="treeitem" aria-selected="false">Muree</li>
<li id="select2-district-result-wasy-16" class="select2-results__option" role="treeitem" aria-selected="false">Newdis</li>
<li id="select2-district-result-lh92-17" class="select2-results__option" role="treeitem" aria-selected="false">newdistric</li>
<li id="select2-district-result-ymav-18" class="select2-results__option" role="treeitem" aria-selected="false">india</li>
<li id="select2-district-result-bxfg-19" class="select2-results__option" role="treeitem" aria-selected="false">uuuuu</li>
<li id="select2-district-result-8leg-20" class="select2-results__option" role="treeitem" aria-selected="false">diaa</li>
<li id="select2-district-result-9tug-21" class="select2-results__option" role="treeitem" aria-selected="false">bagh</li>
<li id="select2-district-result-s5yk-22" class="select2-results__option" role="treeitem" aria-selected="false">bahawalpur</li>
<li id="select2-district-result-gcdg-23" class="select2-results__option" role="treeitem" aria-selected="false">abc</li>
<li id="select2-district-result-mhc7-24" class="select2-results__option" role="treeitem" aria-selected="false">gujrat</li>
<li id="select2-district-result-jvg0-25" class="select2-results__option" role="treeitem" aria-selected="false">okcb</li>
<li id="select2-district-result-t31f-26" class="select2-results__option" role="treeitem" aria-selected="false">ajk</li>
<li id="select2-district-result-p2yc-27" class="select2-results__option" role="treeitem" aria-selected="false">sbbs</li>
<li id="select2-district-result-wbdn-28" class="select2-results__option" role="treeitem" aria-selected="false">vvgzzzggzg</li>
<li id="select2-district-result-9in4-29" class="select2-results__option" role="treeitem" aria-selected="false">18,19,22</li>
<li id="select2-district-result-m3mb-30" class="select2-results__option" role="treeitem" aria-selected="false">chfhb</li>
<li id="select2-district-result-ykgd-31" class="select2-results__option" role="treeitem" aria-selected="false">nz</li>
<li id="select2-district-result-s2q8-32" class="select2-results__option" role="treeitem" aria-selected="false">jhjsdhjrkthjerk</li>
<li id="select2-district-result-hbnk-33" class="select2-results__option" role="treeitem" aria-selected="false">dskhjhdj</li>
<li id="select2-district-result-r2yr-34" class="select2-results__option" role="treeitem" aria-selected="false">dm,sfndmbdnmfbgdfnm</li>
<li id="select2-district-result-z7jh-35" class="select2-results__option" role="treeitem" aria-selected="false">31,32,33,34</li>
<li id="select2-district-result-m09k-37" class="select2-results__option" role="treeitem" aria-selected="false">isb</li>
<li id="select2-district-result-i6fy-38" class="select2-results__option" role="treeitem" aria-selected="false">urdu</li>
<li id="select2-district-result-6480-39" class="select2-results__option" role="treeitem" aria-selected="false">iab</li>
<li id="select2-district-result-36un-40" class="select2-results__option" role="treeitem" aria-selected="false">vaavbaha</li>
<li id="select2-district-result-pb6a-41" class="select2-results__option" role="treeitem" aria-selected="false">1,8</li>
<li id="select2-district-result-ixq5-42" class="select2-results__option" role="treeitem" aria-selected="false">vsvsvsvsvsvvsvsvvs</li>
<li id="select2-district-result-9ura-43" class="select2-results__option" role="treeitem" aria-selected="false">bebusuvuvs</li>
<li id="select2-district-result-sy86-44" class="select2-results__option" role="treeitem" aria-selected="false">2,3</li>
<li id="select2-district-result-cvhr-45" class="select2-results__option" role="treeitem" aria-selected="false">sahiwal</li>
<li id="select2-district-result-eq6b-46" class="select2-results__option" role="treeitem" aria-selected="false">Lahore</li>
<li id="select2-district-result-ne8f-47" class="select2-results__option" role="treeitem" aria-selected="false">kahuta</li>
<li id="select2-district-result-d6c5-48" class="select2-results__option" role="treeitem" aria-selected="false">ksdadadad</li>
<li id="select2-district-result-0trj-49" class="select2-results__option" role="treeitem" aria-selected="false">larkana</li>
<li id="select2-district-result-mw2s-50" class="select2-results__option" role="treeitem" aria-selected="false">abbotabad</li>
<li id="select2-district-result-x3zv-51" class="select2-results__option" role="treeitem" aria-selected="false">PAkistan</li>
<li id="select2-district-result-a7ph-52" class="select2-results__option" role="treeitem" aria-selected="false">PAkistan3</li>
<li id="select2-district-result-v17m-53" class="select2-results__option" role="treeitem" aria-selected="false">ajjjjjjjjk</li>
<li id="select2-district-result-b788-54" class="select2-results__option" role="treeitem" aria-selected="false">uhvhhhvhvhvvuuvhvuvuvh</li>
<li id="select2-district-result-ifxj-55" class="select2-results__option" role="treeitem" aria-selected="false">gchchfhfhfufufuf</li>
<li id="select2-district-result-imfe-56" class="select2-results__option" role="treeitem" aria-selected="false">innnnnnnnnnn</li>
<li id="select2-district-result-g999-57" class="select2-results__option" role="treeitem" aria-selected="false">mmmmmmmmmmmm</li>
<li id="select2-district-result-q0uf-58" class="select2-results__option" role="treeitem" aria-selected="false">ijhchfhfhfhfufhhjhhhhhhhhh</li>
<li id="select2-district-result-iwgt-59" class="select2-results__option" role="treeitem" aria-selected="false">mombai</li>
<li id="select2-district-result-zh8w-60" class="select2-results__option" role="treeitem" aria-selected="false">toba</li>
<li id="select2-district-result-ngwt-61" class="select2-results__option" role="treeitem" aria-selected="false">eesygdgd</li>
<li id="select2-district-result-01c7-62" class="select2-results__option" role="treeitem" aria-selected="false">lanka</li>
<li id="select2-district-result-542v-63" class="select2-results__option" role="treeitem" aria-selected="false">Multan, Muree</li>
<li id="select2-district-result-2mnb-64" class="select2-results__option" role="treeitem" aria-selected="false">isb</li>
<li id="select2-district-result-ktc7-65" class="select2-results__option" role="treeitem" aria-selected="false">is</li>
<li id="select2-district-result-p80n-66" class="select2-results__option" role="treeitem" aria-selected="false">jrjrjrjrjjrjrjrjjrye</li>
<li id="select2-district-result-qkw6-67" class="select2-results__option" role="treeitem" aria-selected="false">baghhh</li>
<li id="select2-district-result-wjsg-68" class="select2-results__option" role="treeitem" aria-selected="false">toba pak</li>
<li id="select2-district-result-syg9-69" class="select2-results__option" role="treeitem" aria-selected="false">damyal</li>
<li id="select2-district-result-8pqk-70" class="select2-results__option" role="treeitem" aria-selected="false">dham</li>
<li id="select2-district-result-p2cn-71" class="select2-results__option" role="treeitem" aria-selected="false">kammmi</li>
<li id="select2-district-result-lios-72" class="select2-results__option" role="treeitem" aria-selected="false">k</li>
<li id="select2-district-result-f78c-73" class="select2-results__option" role="treeitem" aria-selected="false">kgdhdhdhdhhdhdhdjd</li>
<li id="select2-district-result-4w2l-74" class="select2-results__option" role="treeitem" aria-selected="false">bnbnbnbnbnbn</li>
<li id="select2-district-result-0712-75" class="select2-results__option" role="treeitem" aria-selected="false">nathia gali</li>
<li id="select2-district-result-tgz6-76" class="select2-results__option" role="treeitem" aria-selected="false">nizam abad</li>
<li id="select2-district-result-7d0q-77" class="select2-results__option" role="treeitem" aria-selected="false">2,37</li>
<li id="select2-district-result-z6p6-78" class="select2-results__option" role="treeitem" aria-selected="false">rwkt</li>
<li id="select2-district-result-dv7s-79" class="select2-results__option" role="treeitem" aria-selected="false">mandi bahaunddin</li>
<li id="select2-district-result-b65r-80" class="select2-results__option" role="treeitem" aria-selected="false">sahala</li>
</ul>
</span>
</span>
答案 0 :(得分:0)
您使用的ID不返回WebElements列表。尝试使用xpath返回要循环的元素列表并选择所需的一个
$mail