无法触发对硒中UL Li物品的点击

时间:2019-01-13 15:18:46

标签: java selenium selenium-webdriver selenium-chromedriver

我正在使用selenium-java和chrome驱动程序从UL列表中选择元素。 我正在尝试选择“ Sirsa”项目,但没有任何反应。 UL List结构类似于以下内容:

<div class="serachDept" id="names_department"><span class="departmentText">Mumbai</span>
    <ul class="serachDeptOptionList" style="overflow-y: scroll;">
        <li class="serachDeptOptionListElement"><span data-filter-value="Ratia"
                                                    class="srSearchOptionListElementText">Ratia</span></li>
        <li class="serachDeptOptionListElement"><span data-filter-value="Sirsa"
                                                      class="srSearchOptionListElementText">Sirsa</span></li>

        </li>
    </ul>
</div>

我使用Java代码选择了元素:

 WebElement namesList = Hooks.driver.findElement(By.xpath("//*[@id='names_department']//ul"));
    List<WebElement> options = namesList.findElements(By.tagName("li"));
    for (WebElement option : options) {
        option.findElement(By.tagName("Span")).click();  // throws exception
    }
    options.get(2).click();//throws exception

例外:

Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_102'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.45.615291 (ec3682e3c9061c..., userDataDir: C:\Users\HP\AppData\Local\T...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: l}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 71.0.3578.98, webStorageEnabled: true}
Session ID: f68d79a1f74f3dc99c15d8a5f3104789
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
  at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
  at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
  at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
  at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
  at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
  at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
  at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
  at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
  at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
  at com.sg.game.steps_def.CareerMenuItemCheck.dropdown_Weblist_is_enabled_and_Clickable(CareerMenuItemCheck.java:253)

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

解决方案是使用JavascriptExecutor

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", options.get(2));

答案 1 :(得分:0)

尝试一下:

    List<WebElement> elements = 
    Hooks.driver.findElements(By.xpath("/*@id='names_department']//ul/li"));
    for (int i=0; i<elements.size(); i++) {
        if (elements.get(i).getTagName().equals("span") {
        elements.get(i).click()
        }       
    }
    elements.get(2).click();

答案 2 :(得分:0)

您上面的代码看起来有问题。您需要打破循环:

WebElement namesList = Hooks.driver.findElement(By.xpath("//*[@id='names_department']//ul"));
List<WebElement> options = namesList.findElements(By.tagName("li"));
for (WebElement option : options) {
    option.findElement(By.tagName("Span")).click();
    break;
}
//options.get(2).click();//throws exception

或类似下面的内容。

WebElement namesList = Hooks.driver.findElement(By.xpath("//*[@id='names_department']//ul"));
List<WebElement> options = namesList.findElements(By.tagName("li"));
for (WebElement option : options) {
   if("Sirsa".equalsIgnoringCase(option.getText())){
      option.click(); 
      //option.findElement(By.tagName("Span")).click();
      break;
   }
 }   

以上代码至少不会引发命令超时异常。而且,如果您发现点击没有生效,则可以使用javascript来触发点击。

为处理此类情况,众所周知的开源框架QAF提供了一种称为custom component的功能。使用它,您可以为Webelement定制实现。