Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SeleniumWebDriver切换元素_Java_Selenium_Selenium Webdriver_Automation - Fatal编程技术网

Java 使用SeleniumWebDriver切换元素

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

我在网页中有一个切换元素。 使用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" 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);