简体   繁体   English

使用Selenium WebDriver切换元素

[英]Toggle an element using selenium webdriver

I have a toggle element in a webpage. 我在网页中有一个切换元素。 Using selenium i have to toggle right .Am not sure how it can be done using selenium 使用硒我必须正确选择。不知道如何使用硒

Actualy I need to click the following element to toggle 实际上,我需要单击以下元素进行切换

<div class="right">
<input id="app_in" class="cmn-toggle cmn-toggle-round" type="checkbox" value="false">
<label class="preference" tabindex="2" data-preference="inFlag" data-guid="26865MS" for="app_in"></label>
</div>

i tried following code to click the checkbox but getting "Element is not currently visible and so may not be interacted with" error 我尝试按照以下代码单击复选框,但得到"Element is not currently visible and so may not be interacted with" error

 driver.findElement(By.id("app_in")).click();

One possible solution here could be to wait for the element to become visible: 一种可能的解决方案是等待元素变为可见:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement element wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("app_in")));

element.click();

If it does not help, try clicking the element through javascript : 如果没有帮助,请尝试通过javascript单击元素:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM