如果元素不可见,请任何人都可以帮助我如何从下拉列表中选择选项。 这是 html 标签:
<select id="visualizationId" style="width: 120px; display: none;" name="visualization">
<option value="day">Day</option>
<option value="week">Week</option>
<option selected="" value="month">Month</option>
正在研究 selenium webdriver。下面的代码不能正常工作。有没有示例代码来选择不可见元素。
Actions actions1 = new Actions(driver);
WebElement dBox1= ((new WebDriverWait(driver,60)).until(ExpectedConditions.elementToBeClickable(By.id("visualizationId"))));
selectByVisibleText("week");
actions1.moveToElement(dBox1);
actions1.click();
actions1.perform();
使用以下行时也会收到错误:元素当前不可见,因此可能无法与之交互 命令持续时间或超时:32 毫秒
Select se=new Select(driver.findElement(By.id("visualizationId")));
se.selectByVisibleText("Week");
或
se.selectByValue("week");
请查看 html,该元素不可见。任何人都可以建议我如何使元素可见并选择该选项。
最佳答案
在这里,为了选择选项,我尝试使用 javascript 单击元素(javascript 因为它允许您与隐藏元素交互)。下面是代码,大家试试吧。我不确定代码和语法(我不是java人),但你仍然可以使用逻辑。
WebElement elementToSelect = driver.findElement(By.xpath(".//select[@id='visualizationId']/option[text()='Day']")
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementToSelect);
关于javascript - 从下拉元素中选择的选项不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366703/