简体   繁体   English

从下拉元素中选择选项不可见

[英]Select option from dropdown-element is not visible

Please anyone can help me how to select options from the drop down if the element is not visible. 如果该元素不可见,请任何人都可以帮助我如何从下拉菜单中选择选项。 Here is the html tag: 这是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>

Am working on selenium webdriver. 我正在开发硒webdriver。 the below code is not working fine. 下面的代码无法正常工作。 Is there any sample code to select the invisible element. 是否有任何示例代码来选择不可见元素。

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();

When using the below lines also am getting the error: Element is not currently visible and so may not be interacted with Command duration or timeout: 32 millisecond 使用以下行时,也会出现错误:元素当前不可见,因此可能无法与命令持续时间或超时进行交互:32毫秒

Select se=new Select(driver.findElement(By.id("visualizationId")));
se.selectByVisibleText("Week");

or 要么

se.selectByValue("week");

Please see the html and there the element is not visible. 请查看html,那里的元素是不可见的。 Can any one suggest me how to make element visible and select the option. 谁能建议我如何使元素可见并选择选项。

Here, for selecting option, I'm trying to click on element using javascript(javascript because it allows you to interact with hidden elements). 在这里,为了选择选项,我尝试使用javascript(javascript,因为它允许您与隐藏的元素进行交互)单击元素。 Following is the code, give a try. 以下是代码,请尝试。 I'm not sure about the code and syntax(I'm not java guy), still you can use logic. 我不确定代码和语法(我不是Java专家),仍然可以使用逻辑。

WebElement elementToSelect = driver.findElement(By.xpath(".//select[@id='visualizationId']/option[text()='Day']")

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementToSelect);

You don't need the ="" , just selected will work fine 您不需要="" ,只要selected就可以了

Also, how are you supposed the select from a dropdown that isn't visible? 此外,您应该如何从不可见的下拉列表中进行选择? There will be nothing to click 没有什么可点击的

The selected attribute is a boolean attribute. selected属性是一个布尔属性。 The syntax is <option selected> . 语法为<option selected> So Just try this without "" 因此,只需尝试不带""

<option value="month" selected>Month</option>

try this code 试试这个代码

Select se=new Select(driver.findElement(By.id("visualizationId")));

se.selectByVisibleText("week")

my bad its typo mistake misplaced "w" with "W" please try this 我的错是它的错字错误将“ w”与“ W”放错了,请尝试此操作

Select se=new Select(driver.findElement(By.id("visualizationId")));

se.selectByVisibleText("Week");

or 要么

se.selectByValue("week");

You can only interact with elements that are visible on the webpage. 您只能与网页上可见的元素进行交互。 You can surely get the not visible element (or parts of it), but you can't do anything with it. 您当然可以得到不可见的元素(或元素的一部分),但是您不能对其做任何事情。 If you want to interact with this Select element, you first need to make sure it's visible (display attribute of the Select element) so you can interact with it. 如果要与此Select元素进行交互,则首先需要确保它是可见的(Select元素的display属性),以便可以与其进行交互。

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

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