Java 使用SeleniumWebDriver切换元素
我在网页中有一个切换元素。 使用selenium我必须右键切换。我不确定如何使用selenium 实际上,我需要单击以下元素进行切换Java 使用SeleniumWebDriver切换元素,java,selenium,selenium-webdriver,automation,Java,Selenium,Selenium Webdriver,Automation,我在网页中有一个切换元素。 使用selenium我必须右键切换。我不确定如何使用selenium 实际上,我需要单击以下元素进行切换 <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
<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>
这里一个可能的解决方案是等待元素变得可见:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement element wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("app_in")));
element.click();
如果没有帮助,请尝试单击元素:
你试过了吗?我认为你应该诚实地使用WebDriverWait,这个问题并不是关于切换一个元素。这是关于不理解为什么元素不可见。坦率地说,您在理解错误消息方面没有表现出任何努力。标题有误导性,这个问题缺乏研究。对于想知道如何实际切换元素的未来读者来说,这是没有帮助的。所以我投了反对票。如果你修好了,我会把它改成upvote:)
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement element wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("app_in")));
element.click();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);