【问题标题】:Selenium not selecting select option due to javascript call back由于 javascript 回调,Selenium 没有选择选择选项
【发布时间】:2016-08-15 15:04:47
【问题描述】:

我已通读 stackoverflow 上类似问题的答案,但没有一个答案对我有用。大多数答案的具体区别在于 select 元素中的 javascript 回调。我试过选择对象,使用索引、值和文本的定位器,没有一个会选择正确的选项,这都是由于我相信的 javascript 回调

这是我要从中选择的元素:

<select name="ctl0" onchange="javascript:setTimeout('__doPostBack(\'ctl0\',\'\')', 0)" id="ctl0" style="width:205px;margin-left:30px;">
    <option value="0">Option 0</option>
    <option selected="selected" value="1">Option 1</option>
    <option value="2">Option 2</option>

</select>

这是我的代码:

driver.findElement(By.id("ctl0")).click();   
driver.findElement(By.xpath("//select[@id=\"ctl0\"]/option[@value=\"1\"]")).click();

结果没有差异的替代方法:

driver.findElement(By.id("ctl0")).click();
Select select = new Select(driver.findElement(By.id("ctl0")));
WebElement elem = select.getOptions().get(1);
System.out.println(elem.getText());
elem.click();

在你回答之前,我必须根据其他人点击这两个,因为回调似乎欺骗了 Select 对象,因此显式点击和 XPath 定位器。

select 控件的选择在这两种情况下都有效,option 的选择似乎有效,但单击不会导致它被选中。

我尝试了下面的 javascript 选项,同样的问题

    WebElement el = driver.findElement(By.id("ctl0"));
    String jsScript = "showDropdown = function (element) "
            + "{"
            + "    var event; "
            + "    event = document.createEvent('MouseEvents'); "
            + "    event.initMouseEvent('mousedown', true, true, window, 1, 0,0,0,0,false,false,false,false,0,null); "
            + "    element.dispatchEvent(event); "
            + "}; "
            + "showDropdown(arguments[0]);";

    ((JavascriptExecutor)driver).executeScript(jsScript,el);

    WebElement elem = el.findElement(By.xpath(".//option[@value = '1']"));
    System.out.println("Option visible text is " + elem.getText());
    elem.click();

该网站不公开,我无法控制它。请注意,以下解决方案中指定的 initMouseEvent 并不具有所有必需的参数。我想我说得对。 initMouseEvent 现在也被弃用了。

我最初使用 Selenium IDE 来记录操作。这就是它为 Java 导出它的方式:

new Select(driver.findElement(By.id("ctl0"))).selectByVisibleText("Option 1");
driver.findElement(By.cssSelector("option[value=\"1\"]")).click();

读完后-Preserve onchange for a dropdown list when setting the value with Javascript

我试过这个(也有未装饰窗口前缀的功能):

    new Select(driver.findElement(By.id("ctl0"))).selectByValue("1");
    String jsScript = "window.__doPostBack(arguments[0],'');";

    ((JavascriptExecutor)driver).executeScript(jsScript,"ctl0");

但是得到这个:

org.openqa.selenium.WebDriverException: TypeError: window.__doPostBack is not a function (WARNING: The server did not provide any stacktrace information)

想法?

【问题讨论】:

  • 您是否尝试过使用Select 类??如果试过了,你能分享一下吗??
  • 你能在公共 URL 上复制这个问题吗?

标签: java selenium xpath selenium-webdriver


【解决方案1】:

Selenium 提供Select 类来处理下拉列表,方法如下:

WebElement el = driver.findElement(By.id("ctl0"));
Select select = new Select(el);

注意:我建议您尝试使用上述方法之一从下拉列表中选择选项,而不是使用.click()

已编辑:如果不幸的是上述方法不起作用,您可以尝试使用JavascriptExecutor,如下所示:-

WebElement el = driver.findElement(By.id("ctl0"));

((JavascriptExecutor)driver).executeScript("showDropdown = function (element) {var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; showDropdown(arguments[0]);",el);

el.findElement(By.xpath(".//option[@value = '1']").click();

【讨论】:

  • 是的,正如我原来的问题所述,我完全意识到这一点。但是,正如我更新的问题中所述,Select 方法也不起作用。
  • @csDave 好的,您可以尝试使用JavascriptExecutor 然后..尝试更新答案并告诉我
  • 我试过了,没区别。也许问题出在 Ubuntu 上的 Firefox?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 2021-12-06
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多