【问题标题】:Selenium WebDriver - hidden select and anchor [duplicate]Selenium WebDriver - 隐藏的选择和锚点[重复]
【发布时间】:2012-09-11 13:47:51
【问题描述】:

可能重复:
Selenium WebDriver - get options from hidden select

我在选择一页时遇到了一个大问题。

代码:

<select name="fw3k_ad_input_et_type_group" class="" 
            id="_id_fw3k_ad_input_et_type_group" 
            onchange=" eurotax.change_type_group( this.value ); " 
            style="display: none; ">

        <option value="0"> --- odaberite tip --- </option>
        <option value="-1" class="special">> nema mog tipa  </option>
        <option value="16390">CD</option>
        <option value="17605">S</option>
        <option value="17636">SE</option>

</select>
<a href="" class="fs_item" id="fs_item_0" style=""> --- odaberite tip --- </a> 


查看:

http://imageshack.us/f/7/screenshotfrom201209111.png/

Select 被隐藏,href="" 是可见部分,它会根据所选选项更改其文本。

我真的不知道如何管理它。我可以使用 JavascriptExecutor 获取所有选项,并且可以使用 a.click() 查看下拉框,但我不知道如何单击某些选项。

我尝试使用 Select 类和 .getOptions() 方法,但它不适用于隐藏选择,我无法更改 href="" 文本。

:(

请帮我举个例子。

【问题讨论】:

  • 你想点击什么元素? Selenium 不会与隐藏元素交互。
  • 我的列表中有选项 [--- odaberite tip ---, CD, S, SE, > nema mog tipa] 我需要选择例如“CD”并执行类似的选择.selectByVisibleText("CD"); (选择选项并单击)请参阅:imageshack.us/f/7/screenshotfrom201209111.png
  • 很可能,通过使用 JavaScript 是可能的

标签: select drop-down-menu selenium webdriver options


【解决方案1】:

对这个问题有点困惑,但你试过了吗

WebElement element = driver.findElement(By.id("fw3k_ad_input_et_type_group"));
Select select = new Select(element);

然后使用任一

select.selectByValue(value);
select.selectByVisibleText(text);
select.selectByIndex(index);

【讨论】:

  • 我不能使用 Select select = new Select(element);异常:线程“AWT-EventQueue-0”org.openqa.selenium.ElementNotVisibleException 中的异常:元素当前不可见,因此可能无法与之交互
  • 选择不可见。它在后台工作。锚是可见部分,它会根据所选选项更改其文本!
  • 你在测试页面使用jQuery吗?如果是这样,您可以使用 JavascriptExecutor 并执行 $("#_id_fw3k_ad_input_et_type_group").val('16390');
  • 我确实用 JavascriptExecutor 解决了这个问题! JavascriptExecutor js = (JavascriptExecutor) 驱动程序; js.executeScript("document.getElementById('_id_fw3k_ad_input_et_type_group').style.display='';");
【解决方案2】:

第一种方式: 使用相同的js单击任何元素都不是问题。如您所知,如何获得任何选项,最后的操作是执行单击。 这应该适合你:

WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);

第二种方式

String cssSelector= ...//i gave them in your previous question
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

第三种方式: 使用操作生成器,高级用户操作 API。你可以阅读它here 代码会是这样的:

WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();

您还可以获取一些其他信息here 希望这对您有所帮助)

【讨论】:

    【解决方案3】:
    driver.findElement(By.name("_id_fw3k_ad_input_et_type_group")).sendKeys("16390");
    

    为我的工作非常相似。

    【讨论】:

    • 不错的尝试,但出现异常:线程“AWT-EventQueue-0”中的异常 org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
    • 谢谢 Stilltorik 它在我的情况下工作......
    猜你喜欢
    • 2014-04-08
    • 2023-03-02
    • 2012-09-02
    • 2012-08-15
    • 2012-09-03
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多