2017-08-28 78 views
2

对于我的测试之一,我需要按下一个自举开关按钮,它具有以下HTML:如何使用Selenium Java Webdriver切换引导程序开关?

<div class="bootstrap-switch-container" style="width: 93px; margin-left: -31px;"> 
    <span class="bootstrap-switch-handle-on bootstrap-switch-primary" style="width: 31px;">ON</span> 
    <span class="bootstrap-switch-label" style="width: 31px;">11111&nbsp;</span> 
    <span class="bootstrap-switch-handle-off bootstrap-switch-default" style="width: 31px;">OFF</span> 
    <input name="form:taskDone" id="input_form:taskDone" type="checkbox"> 
</div> 

我尝试使用

By TASK_DONE_SWITCH_LOCATOR = By.id("input_form:taskDone"); 
taskDoneSwitch = wait.until(ExpectedConditions.elementToBeClickable(TASK_DONE_SWITCH_LOCATOR)); 
taskDoneSwitch.click(); 

然后我收到以下异常:

Timed out after 5 seconds 
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 15:53:48' 
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_131' 
Driver info: driver.version: unknown 
org.openqa.selenium.TimeoutException 
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:220) 
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:188) 
    at hu.dual.webapp.customerservice.note.AbstractNoteTest.refreshEditPageTaskElements(AbstractNoteTest.java:161) 
    at hu.dual.webapp.customerservice.note.EditNoteTest.setTaskToDone(EditNoteTest.java:76) 

堆栈跟踪指向此行:

taskDoneSwitch = wait.until(ExpectedConditions.elementToBeClickable(TASK_DONE_SWITCH_LOCATOR)); 

然后它不会点击。我怎样才能点击它?我尝试使用其他字段和ExpectedConditions.visibilityOfElementLocated,但我仍然无法点击它。

回答

2

:在id属性可能会导致问题。尝试使用部分ID

By TASK_DONE_SWITCH_LOCATOR = By.cssSelector("[id*='input_form']"); 

或者

By TASK_DONE_SWITCH_LOCATOR = By.cssSelector("[id*='taskDone']"); 

或者两者

By TASK_DONE_SWITCH_LOCATOR = By.cssSelector("[id*='input_form'][id*='taskDone']"); 

Edit

您也可以尝试父元素上点击

By.className("bootstrap-switch-container"); 
+0

这不会抛出任何异常,但不会点击按钮!感谢您的关注! – brightpants

+1

@brightpants也许你需要点击其他元素?如'By.className(“bootstrap-switch-primary”);'或'By.className(“bootstrap-switch-default”);' – Guy

+0

我试过了他们,但是这些消息是: 未知错误:元素 ...在点(218,686)处不可点击。其他元素将会收到点击:

...
brightpants

1

可以通过增加从5等待到30

尝试,如果还是不行用下面的代码试试:

WebElement taskDoneSwitch = driver.findElement(TASK_DONE_SWITCH_LOCATOR); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", taskDoneSwitch); 

希望它会帮助你:)

+0

好吧,30年代没有解决它,并且你建议的代码给我这个例外: 未知的错误:Runtime.evaluate抛出异常:SyntaxError:意外的标记: – brightpants

相关问题