gpt4 book ai didi

java - 由于 javascript 回调,Selenium 未选择选择选项

转载 作者:行者123 更新时间:2023-11-30 07:12:38 24 4
gpt4 key购买 nike

我已经阅读了 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 定位器。

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

我尝试了下面的 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)

想法?

最佳答案

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

关于java - 由于 javascript 回调,Selenium 未选择选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38957706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com