无法使用WebDriver单击每个选项为单选按钮的下拉菜单

时间:2014-03-04 21:35:58

标签: selenium xpath

我在WebDriver中从下拉列表中选择一个选项时出现问题,其中选项为type=radio,如下所示:

<input id="ui-multiselect-companyListSelect-option-2" type="radio" title=" Display Deleted " value="deleted" name="multiselect_companyListSelect" aria-selected="true">
<span> Display Deleted </span>

我正在使用以下XPath表达式:

selectDisplayDeleted = //input[@name='multiselect_companyListSelect']/../span[text()=' Display Deleted '] and executing selectDisplayDeleted.click() on this element.

我收到以下异常:

Caused by: org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=32.0.1700.76)
  (Driver info: chromedriver=2.8.241075,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 12 milliseconds
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
Session ID: f667a12c7452cc7d9cf69a07523f1ed4
Build info: version: '2.36.0', revision: '9b50f805d0a53506a5ee6a55be8669f106d8d34e', time: '2013-10-17 11:09:21'
System info: host: 'gc-wk-rbah-01', ip: '10.10.20.75', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:77)
    at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement$1.invoke(EventFiringWebDriver.java:327)
    at $Proxy2.click(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:340)
    at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

2 个答案:

答案 0 :(得分:0)

尝试两次点击。首先是下拉列表,然后是选项值。根据您提供的xpath,第一次单击将在元素输入[@ name ='multiselect_companyListSelect']上,然后是选项值。

答案 1 :(得分:0)

嗯,你的XPath对我来说有点过于宽松,使用following-siblingpreceding-sibling轴来获取span可能会更好。你的跨度文本中似乎还有一些可能导致问题的空白?

//span[contains(text(), 'Display Deleted'][preceding-sibling::input/@name eq 'multiselect_companyListSelect']